Like Tree5Likes

Using arrays in C

This is a discussion on Using arrays in C within the C Programming forums, part of the General Programming Boards category; Someplace before line #10, you need to assign i the value of 0. Otherwise the while() loop will be useless. ...

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Someplace before line #10, you need to assign i the value of 0. Otherwise the while() loop will be useless. Also, assign 0 to the value of numbers[0], by:
    Code:
    int i=0;
    numbers[SIZE]={0}; //sets all elements of the array to 0
    Inside the while loop (the last line of it probably), you need to increment the variable i: ++i;

    Now the index i and the test, will be OK.

    Put that into place, and see how far that gets you!

  2. #17
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Code:
    int main (void)
    {
     int nums[SIZE]={1};
     int count=0;
     int i=0;
     int tests;
     
     while (nums[i] >= 0)
     {
      printf("Enter test score %d: ", nums[i]);
      scanf("%d", &nums[i]);
      i++;
     }
     
     for (i=0; i<count; i++)
     {
      printf("Number of test scores entered: %d", tests);
      printf("Tests scores entered: %d", count);
     }
     return 0;
    }
    Okay so I did that and now it runs so thanks! However, it asks for "enter test score 0, " and doesn't increment to "test score 1, 2, etc"

  3. #18
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Sorry, I meant it starts at "enter test score 1, then 0" and doesn't change from 0.. help?

  4. #19
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Code:
    printf("Enter test score %d: ", nums[i]);
    You probably want
    Code:
    printf("Enter test score %d: ", i+1);
    This way you are not relaying back to the screen the data in nums[i] but the actual value of i instead.

  5. #20
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Code:
    #include <stdio.h>
    #define SIZE 35
    int main (void)
    {
     int nums[SIZE]={0};
     int count=1;
     int i=0;
     
     while (nums[i] != -1)
     {
      printf("Enter test score %d: ", count++);
      scanf("%d", &nums[i]);
      i++;
     }
     
     for (i=0; i<count; i++)
     {
      printf("Number of test scores entered: %d", count);
      printf("Tests scores entered: %d", count);
     }
     return 0;
    }
    I have this now and it prompts for test score 1, 2, 3.. but won't stop at -1?

  6. #21
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    643
    Write it out on paper before you come to a conclusion that you are completely stumped.
    For example you go into the while loop the first time.
    enter test score 1:
    scans -1 into nums[0]
    then increments i to 1
    now while loop checks if nums[1]!=-1 instead of nums[0]
    You are essentially entering in your number than incrementing i before the while loop can check to see if it is valid condition.

  7. #22
    Registered User
    Join Date
    Dec 2012
    Posts
    289
    instead of
    Code:
    while (nums[i] != -1)

    i would use a for loop like you did in line 16
    and in the for loop have it check the input for a -1 entry, and break out of the loop


    oh and BARNEY, for someone like me that has done programs with MANY MANY lines of input, when your taking in lines, from lets say a data sheet, being able to see that the program wants says line 25, or value 25, it is easier then "enter next line" because that way you dont get to your last line, and the program either is waiting for another or done before you have entered the last line, all because you skipped a line on pages of a print out by accident.

    having to add one single line to a code, to say "enter line %i" isnt "over complicated" and far from "a pain the ass for the programmer"

    with MOST users today, you can NEVER over simplify a program, especailly any console "text based" program.
    Last edited by Crossfire; 02-12-2013 at 03:26 PM.

  8. #23
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Not all console programs are batch programs.
    I don't know what a batch program is. Are they programs that make a mess on stdout when stdin's connected to a non-interactive device?

    Well, they're both sentinel controlled loops in the end, so I guess it doesn't matter.
    Except that if you handle "-1" you need to handle EOF as well.

    edit:
    MANY MANY lines of input
    If you are writing programs that read many lines of input, why are you reading them interactively?

    having to add one single line to a code, to say "enter line %i" isnt "over complicated" and far from "a pain the ass for the programmer"
    It's about rewriting a program which uses all whitespace as a delimiter to a program that uses newlines as a delimiter and eats other whitespace. Prompts really only work when your input is line-based and strictly interactive.
    Last edited by Barney McGrew; 02-13-2013 at 12:37 AM.

  9. #24
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by krendo01 View Post
    I'm sorry, size change to what?
    For 35 scores, SIZE of 35 is fine. Just remember that your valid array index numbers range from 0 to 34 ONLY.

    Delete the comma and nums[i] from this line of code:

    Code:
    printf("Enter test score %d: ", nums[i]);
    You don't want to print out the score, because the score hasn't been entered yet! When the user enters the score, the score will be echoed to the screen - so there's no need to echo it onto the screen yet again.

    Make sense?
    Last edited by Adak; 02-13-2013 at 01:13 AM.

  10. #25
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    Can anyone please tell me why my loop won't break out when I enter -1??? And how I can fix it?? Thanks!
    Code:
    #include <stdio.h>
    #define SIZE 35
    int main (void)
    {
     int nums[SIZE]={0};
     int count=1;
     int i;
     
     while (nums[i] != -1)
     {
      printf("Enter test score %d: ", count++);
      scanf("%d", &nums[i]);
      i++;
      if (i == -1)
      break;
     }
     
     for (i=0; i<count; i++)
     {
      printf("Number of test scores entered: %d", count);
      printf("Tests scores entered: %d", count);
     }
     return 0;
    }

  11. #26
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by krendo01 View Post
    Can anyone please tell me why my loop won't break out when I enter -1??? And how I can fix it?? Thanks!
    I found an earlier message which tells you the problem. You might want to take a look here:

    Using arrays in C

  12. #27
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    I still can't fix it :/

  13. #28
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    I think the variable involved in the loop condition should be a different variable; don't try to use the array. You should only add the input variable to the array after you know it's OK. If you do that the test for the loop condition is easier.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modifying parallel arrays to arrays of structures
    By xkohtax in forum C Programming
    Replies: 7
    Last Post: 07-28-2011, 01:07 AM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. separating line of arrays into array of arrays
    By robocop in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 01:43 AM

Tags for this Thread


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