Thread: Arrays

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    24

    Arrays

    I have to make an array that follows the criteria below. It reads in the numbers from the numbers in quotes below. The dark green are the scores I want to read in from the array, the blue is the max contestants and the red is the max judges. My array kept failing and crashing the output window.

    A. An array to store all the contestant’s scores.
    i. type int
    ii. should be 2 dimensional, of size MAX_CONTESTANTS x MAX_JUDGES
    iii. index [i][j] should store contestant i’s score from judge j. (contestant and judge
    numbers start from 0)


    3
    2 4 10
    85 90 100 98
    78 82 47 77

    3 5 12
    85 90 100 98 76
    78 82 47 77 75
    94 6 97 98 99

    2 3 15
    80 20 50
    78 22 53

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Practical programming includes a lot of common sense problem solving, and this is an example.

    Post up your attempt to solve this, and let's see where you went awry. You should be able to figure it out, but maybe the in's and out's of allocating the array is the problem.

    But let's see what's the problem.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    I couldnt get the 2 d array to work however I did get it to read in the numbers and print them in the output correctly using a while loop and 1 d array

    Code:
    int main() {
        
        int num_shows, num_contestants, num_judges, sd_threshold;
        double average;
        
        // Open the text file.
        FILE* ifp = fopen("ucfidol.txt", "r");
        
        //scanning info from text file
        fscanf(ifp, "%d", &num_shows);
        fscanf(ifp, "%d%d%d", &num_contestants, &num_judges, &sd_threshold);
        
        //declaring array size by text file
        int score[num_contestants][num_judges];
        int i, j;
        
        //populates 2d array with numbers from text file
        for (i=0; i<num_contestants+1; i++) 
          for (j=0; j<num_judges; j++)
            fscanf(ifp, "%d", &score[i][j]);

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Why is it "num_contestants+1" in the "i" array but "num_judges" in the "j" array?

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    Quote Originally Posted by dubbin240 View Post
    I couldnt get the 2 d array to work.
    After you populate the 2d array with values what happens? Does the array have any values?

    Hunter

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    That was a typo...it should be judge in the i array and contestants in the j..the way the file reads in...and when I run it shows a blank output window then says it has stopped working

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    Do you mind posting your input file and your entire main function. That'll make it easier to find the problem.

    Hunter

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    int main() {
        
        int num_shows, num_contestants, num_judges, sd_threshold;
        double average;
        
        // Open the text file.
        FILE* ifp = fopen("ucfidol.txt", "r");
        
        //scanning info from text file
        fscanf(ifp, "%d", &num_shows);
        fscanf(ifp, "%d%d%d", &num_contestants, &num_judges, &sd_threshold);
        
        //declaring array size by text file
        int score[num_contestants][num_judges];
        int i, j;
        
        //populates 2d array with numbers from text file
        for (i=0; i<num_judges; i++) {
          for (j=0; j<num_contestants; j++)
            fscanf(ifp, "%d", &score[i][j]);
             printf("%d",score[i][j]);
            //printf("%d",score[num_c]);      //testing the file input
            printf(" "); 
      }
           
    
        
       
        // Close the file.
        fclose(ifp);
        
            system("PAUSE");
        return 0;
    }
    Attachment 10432

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    okay so I found the problem, now it's time for you to find it

    I would look at how you declare your array:
    Code:
    int score[num_contestants][num_judges];
    Then look at the order of variables inside your for loops:
    Code:
    for (i=0; i<num_judges; i++) {
          for (j=0; j<num_contestants; j++)
            fscanf(ifp, "%d", &score[i][j]);
             printf("%d",score[i][j]);
            //printf("%d",score[num_c]);      //testing the file input
            printf(" "); 
      }
    Also use braces after every for loop, it will make your issue easier to see.

    Hope that helps,
    Hunter

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I don't think you can declare a 2-d array dynamically sized like that.

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    sure you can:
    the initial size of the array is set to the bounds passed in for num_contestests and num_judges, and the array is filled with zeros.

    Hunter

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    okay so I found the problem, now it's time for you to find it
    Ok so when you say you figured it out you actually ran it and it worked? I changed the int score to have judges for i and contestants for j...I also rearranged some of the variables several times and still didn't run properly

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    I ran it and it worked how you described it in your previous post. But is judges supposed to be for i? Look at your array declaration.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    24
    O ok yea it should be contestants...I'm just not really familiar with arrays and I don't really get how it reads in or stores the numbers

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Google a C array tutorial on the net, and bone up on the subject. It's critical to understand it, before you can work with them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM