Like Tree1Likes
  • 1 Post By whiteflags

Lost and need help with C Program

This is a discussion on Lost and need help with C Program within the C Programming forums, part of the General Programming Boards category; Hi I am newbie to the boards and c programming. I need assistance in creating arrays. The array has to ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    4

    Lost and need help with C Program

    Hi
    I am newbie to the boards and c programming.

    I need assistance in creating arrays. The array has to be able to hold 20 IDs (Numbers 1-20) and associated 20 associated ratings (Scaled 1-100), however my assignment asks i be prompted to enter id/rating and really only requires me to enter in 10. It must also terminate when a 0 is entered.

    (That part I have, I think, maybe not properly but it works.)


    My issue is then getting them to appear in chart that shows
    ID Rating

    I can't figure out the for loop.

    Nothing shows!

    Here is my code:
    Code:
    int main(void)
    {
        int id[20], rating[20];
        int i;
        
    
    // Enter ID and Rating, terminated if ID entered is a zero
        while (id[i]!=0){
         printf("Enter ID:");
        scanf("%i", &id[i]);
        printf ("Enter rating:");
        scanf ("%i",&rating[i]);
        }
     
     
        //Table build to show id and rating
        printf("\nID     Rating\n");
        printf("----------     ---------\n");
        for ()              (((((This is the part I cant do to get the table to build and print)))))
        printf("%i %i\n",id[i],rating[i]);
     
        return 0;

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,741
    O_o ;;

    How do you count from 0 to 20?
    Adak likes this.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,438
    Your code has a few problems.

    Firstly, the array id is not initialised, so all values of its elements are indeterminate. Similarly, i is not initialised. The first thing your code does is compare id[i] with zero. The value of i is indeterminate, and the values in id are indeterminate. So your loop can do anything you can imagine (including nothing).

    If you are not filling in the array (eg only reading 10 values, but have an array that can hold 20) then it is necessary to keep track of how many values have actually been read.

    One you correct the problems I've just mentioned, look up the general syntax of a for loop. The changes necessary to correct the problems I've mentioned will also have a benefit of meaning your code now has information relevant to starting the loop, iterating, and terminating the loop.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by whiteflags View Post
    O_o ;;

    How do you count from 0 to 20?
    Huh? Sorry I don't get what you mean?
    When it asks me my ID I use 1 (Then enter rating), next time 2, rating...And so on for the ten I have been given. Then I enter a zero to stop.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,741
    If id[i] doesn't correspond with rating[i], then you should make it so -- it's easier. Parallel array - Wikipedia, the free encyclopedia

  6. #6
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    Quote Originally Posted by grumpy View Post
    Your code has a few problems.

    Firstly, the array id is not initialised, so all values of its elements are indeterminate. Similarly, i is not initialised. The first thing your code does is compare id[i] with zero. The value of i is indeterminate, and the values in id are indeterminate. So your loop can do anything you can imagine (including nothing).

    If you are not filling in the array (eg only reading 10 values, but have an array that can hold 20) then it is necessary to keep track of how many values have actually been read.

    One you correct the problems I've just mentioned, look up the general syntax of a for loop. The changes necessary to correct the problems I've mentioned will also have a benefit of meaning your code now has information relevant to starting the loop, iterating, and terminating the loop.
    Wow guess I'm not even close as I thought. I have a lot to figure out with your reply but I truly appreciate the quick response! Thank you

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    4
    I can't figure out how I get the items (ID, Rating) I enter to save to their arrays?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Would you do me a favor and change %i to %d?

    Now let's talk about the variable i: before the while loop, initialize it to 0 - then inside the while loop, increment it - i++.

    See if that doesn't get your arrays working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cash register program? I'm lost
    By Rhino932 in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2011, 07:57 AM
  2. Replies: 2
    Last Post: 12-19-2005, 05:57 PM
  3. Files Program... really lost
    By thynksheraze in forum C++ Programming
    Replies: 2
    Last Post: 09-25-2003, 07:30 PM
  4. Lost with this program....
    By Jamina in forum C++ Programming
    Replies: 3
    Last Post: 08-09-2003, 09:21 AM
  5. Grades program has me lost
    By adrea in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2002, 07:35 PM

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