Thread: Loops only going through twice??

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    26

    Loops only going through twice??

    Again, I'm making a program that asks for some data then spits out the mean, median, mode and range. However, my mode fuction doesn't seem to be working as intended, as when I use a debugger, I can see that only the first two values are being used in data_common (aka data_common[0]and data_common[1] contain the correct values but data_common[2] is 0, which is weird as that isn't what is should be set to as default).
    Any help on what's going on?
    Code:
    double mode(double data[], int data_elements){
        double data_common[100][2] = { //stores what variables are stored and how many instances there are
                                        {1073709551614}, //random number, incredibly unlikely someone will actually input this
                                        {1073709551614}
                                     };
        double most_often = 0; //stores how many times the most common variable shows up
        double mode1 = 0; //return value, what variable is the most common
        for(int i = 0;i < data_elements;i++)
        {
            for(int j = 0;j < data_elements;j++)  //check if value in data[i] is already in data_common
            {
                if(data_common[j][0] == data[i])
                {
                    data_common[j][1]++; //if it already exists, add one to how many times it has shown up
                }
                if(data_common[j][0] == 1073709551614) //execute if data_common[j] hasn't been given a value
                {
                    for(int x = 0; x < data_elements;x++)
                    {
                        if(data[i]==data_common[x][0]) //again check if a different value in the array is already storing it
                            goto LOOP; //if it is, then exit the loop
                    }
                    data_common[j][0]=data[i]; //if not, give data_common[j] the value of data[i]
                    data_common[j][1]=1; //make sure we know that that value has shown up once before
                }
            LOOP:;
            }
        }
        for(int i = 0; i < data_elements;i++) //check every variable
        {
            if(data_common[i][1] > most_often) //if value is greater than current highest
            {
                most_often = data_common[i][1]; //set new highest
                mode1 = data_common[i][0]; //make sure we get the number which has the most occurences
            }
        }
        return mode1; //return the number with most occurences
    }
    main.h
    main.c
    Last edited by YayIguess; 09-30-2015 at 10:56 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Two dimensional arrays in C are declared in column major order, which means that
    > double data_common[100][2]
    Actually means 100 columns by 2 rows of doubles. So, the first 2 elements are the absurdly big numbers you picked to mean UNUSED (and are not random, despite comments to the contrary).

    It is also true that in C, arrays must be initialized. If you initialize, but give fewer than the necessary number of values, the rest of the values are 0.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > if(data_common[j][0] == 1073709551614)
    But only data_common[0][0] and data_common[0][1] contain your magic number. All the other elements of the array contain zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    Quote Originally Posted by whiteflags View Post
    Two dimensional arrays in C are declared in column major order, which means that
    > double data_common[100][2]
    Actually means 100 columns by 2 rows of doubles. So, the first 2 elements are the absurdly big numbers you picked to mean UNUSED (and are not random, despite comments to the contrary).

    It is also true that in C, arrays must be initialized. If you initialize, but give fewer than the necessary number of values, the rest of the values are 0.
    *shocked!!!!*
    C is row-major order.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Salem View Post
    *shocked!!!!*
    C is row-major order.
    I give up on understanding this.

    No matter what I learn/hear/read, I get it backward.

  6. #6
    Registered User
    Join Date
    Aug 2015
    Posts
    26
    Thanks for your guy's help, got it working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ programing , Loops , infinite loops issue
    By Ibrahim Lawleit in forum C++ Programming
    Replies: 15
    Last Post: 07-14-2014, 03:41 PM
  2. converting for loops to while loops
    By nabhatt in forum C Programming
    Replies: 3
    Last Post: 02-16-2012, 09:45 PM
  3. While loops
    By JonnyFrond in forum C Programming
    Replies: 2
    Last Post: 08-17-2011, 06:09 PM
  4. Replies: 3
    Last Post: 06-01-2011, 04:19 PM
  5. loops, menu loops
    By gloworm in forum C Programming
    Replies: 17
    Last Post: 04-12-2010, 07:59 PM