Need help with 'C' Program (arrays)

This is a discussion on Need help with 'C' Program (arrays) within the C Programming forums, part of the General Programming Boards category; Hey everyone, I know this is going to end up being a really stupid mistake, but im taking a C ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Need help with 'C' Program (arrays)

    Hey everyone,
    I know this is going to end up being a really stupid mistake, but im taking a C class and Im stuck on my program, im trying to get this program to look like this:

    Please enter the number of results per experiment: 3
    Please enter the number of experiments: 2

    Please enter result 1 for experiment 1: 1.0
    Please enter result 2 for experiment 1: 2.0
    Please enter result 3 for experiment 1: 3.0

    Please enter result 1 for experiment 2: 4.0
    Please enter result 2 for experiment 2: 5.0
    Please enter result 3 for experiment 2: 6.0

    Result 1 Result 2 Result 3 Exper. Average
    --------------------------------- ------------
    Exper. 1: 1.0000 2.0000 3.0000 2.0000
    Exper. 2: 4.0000 5.0000 6.0000 5.0000


    but my output at the bottom is coming out with numbers different than what they should be...(I.E. 1.85 for 1, 2.124 for 3).Any ideas what im doing wrong?


    thanks
    you all rock

    Code:
    #include <stdio.h> 
    
    
    
    #define MAX_RESULTS 6 
    #define MAX_EXPERS 10 
    
    
    int 
    main(void) 
    { 
    int count = 0; 
    int results; 
    int experiments = 0; 
    int excount = 0; 
    int rescount = 0; 
    
    
    float fresults[MAX_RESULTS][MAX_EXPERS]; 
    
    
    
    
    count = 0; 
    
    
    results = 1; 
    
    while(results != 0) 
    { 
    
    printf("Please enter the number of results per experiment (No More Than 6): "); 
    printf("\t\t\t\t\t\t\t\tOr '0' to quit "); 
    scanf("%d", &results); 
    
    if(results > 0) 
    { 
    printf("Please enter the number of experiments(No More than 10): "); 
    scanf("%d", &experiments); 
    
    
    if(results > MAX_RESULTS || experiments > MAX_EXPERS) 
    { 
    printf("Please check to make sure your values aren't greater than the defined limits!\n\n\n"); 
    } 
    
    else 
    { 
    
    for (excount = 0; excount < experiments; excount++) 
    { 
    
    printf("\n"); 
    
    for (rescount = 0; rescount < results; rescount++) 
    { 
    
    printf("Please enter result %d for experiment %d: ", (rescount + 1), (excount +1)); 
    
    scanf("%lf", &fresults[rescount][excount]); 
    
    
    
    } 
    
    } 
    printf("\n\n"); 
    } 
    
    
    for(rescount = 0; rescount < results; rescount++) 
    { 
    printf("Result %d\t", (rescount + 1)); 
    
    } 
    printf("Exper. Average"); 
    printf("\n"); 
    for(rescount = 0; rescount < results; rescount++) 
    
    { 
    printf("--------- \t", (rescount + 1)); 
    
    } 
    printf("---------"); 
    printf("\n"); 
    
    
    } 
    /* note to self: Add loop to output more than 1 row once you get this to work */ 
    for(rescount = 0; rescount < results; rescount++) 
    { 
    printf("%f\t", fresults[][0]); 
    
    } 
    printf("\n"); 
    } 
    return(0); 
    }

  2. #2
    back? dbaryl's Avatar
    Join Date
    Oct 2001
    Posts
    597
    OK, first, a couple of things to get you started:
    Code:
    /* note to self: Add loop to output more than 1 row once you get this to work */
    for(rescount = 0; rescount < results; rescount++)
    {
      printf("%f\t", fresults[][0]);
    }
    That part above should be changed to the following:
    Code:
    
        for(rescount = 0; rescount < results; rescount++)
          printf("%f\t", fresults[rescount][0]);
    You forgot the loop-counter index there, and now you can add the outer loop like you intended (in the comment).

    Another thing, the line that reads:
    Code:
    printf("Please enter the number of results per experiment (No More Than 6): ");
    should really say:
    Code:
    printf("Please enter the number of results per experiment (No More Than %d): ", MAX_RESULTS);
    change this for maximum experiments as well.

    Also, in this part of code, the part I put in bold doesn't do anything and is completely unnecessary:
    Code:
    for(rescount = 0; rescount < results; rescount++)
    {
    printf("Result %d\t", (rescount + 1));

    Make some of these changes, and have a shot at the "/* note to self: Add loop to output more than 1 row once you get this to work */" part of the program.

    Good luck, and if anything comes up, just ask

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  2. Replies: 3
    Last Post: 01-14-2003, 09:34 PM
  3. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  4. Replies: 0
    Last Post: 10-29-2001, 10:40 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21