Stopping an Array from Taking More Values
Author |
Message |
solblade05
|
Posted: Wed Nov 26, 2008 7:52 pm Post subject: Stopping an Array from Taking More Values |
|
|
Hey Guys,
Its been a long time since I posted here, anyways the problem I have is that I have initiated an array and ask the user to enter values using the for loop. Now I want the user to enter however many values he/she wishes to, for which there has to be a sentinel value which stops the array from taking anymore values from the user and stores the values it already has taken.
This is what I have, I have a feeling that I am not really on track here:
Just focus on Case 1...
code: |
#include <stdio.h>
#include <math.h>
/* #define SIZE 50 */
main ()
{
int menuchoice, i, sent = 000, SIZE = 50;
double data[50];
printf ("This Program is Done By Jawad Khan, 500277777\n\n");
puts ("1) Enter Data");
puts ("2) Compute and Display Statistics");
puts ("3) Quit");
puts ("\nPlease Choose One of the Above Choices");
scanf ("%d", &menuchoice);
switch (menuchoice)
{
case 1:
printf ("\nYou Have Chosen to Enter Data\n");
for (i=0; i < SIZE; i++)
{
printf ("\nPlease Enter Numbers in Sequence: ");
do
{
scanf ("%d", &data[i]);
} while (data[i] != sent);
}
break;
case 2:
printf ("\nYou Have Chosen to Display the Calculated Statistics\n");
break;
case 3:
printf ("\nYou Have Chosen to Quit\n");
break;
}
}
|
Thanks in advance |
|
|
|
|
|
Sponsor Sponsor
|
|
|
md
|
Posted: Wed Nov 26, 2008 8:36 pm Post subject: RE:Stopping an Array from Taking More Values |
|
|
Pseudocode:
code: | i = 0
read number into input
while i < SIZE && input != stop_value
store value in array at i++
read number into input
|
|
|
|
|
|
|
Okapi
|
Posted: Wed Nov 26, 2008 8:45 pm Post subject: RE:Stopping an Array from Taking More Values |
|
|
Not sure if I am understanding this correctly but just do
while(input != sent){
array[index] = input;
}.
From what I can see, in your solution your for loop will loop 50 times regardless of a sentinel value being entered and will print "Please enter blah blah" 50 times.
edit: left tab open too long, md replied already and his makes more sense because mine doesn't check to see if you go past the array limit. |
|
|
|
|
|
solblade05
|
Posted: Wed Nov 26, 2008 9:03 pm Post subject: RE:Stopping an Array from Taking More Values |
|
|
Hey,
Thanks for the reply, but I cannot get the while loop to work. Regardless of where I put it, it continuously reads 50 numbers from the user, even if I enter the stop value. |
|
|
|
|
|
Okapi
|
Posted: Wed Nov 26, 2008 9:18 pm Post subject: RE:Stopping an Array from Taking More Values |
|
|
your input is an array of ints correct?
So
while (index < SIZE && array[index] != 000){
scanf ("%d", &array[index]);
index++;
}
Could try this: Not sure if it will work at all:
while(index < SIZE && (array[index++] = getchar() - '0') != 000{
}
bah neither of these work. To get it to work for single figures is easy but double figures or more and it's annoying |
|
|
|
|
|
solblade05
|
Posted: Wed Nov 26, 2008 10:18 pm Post subject: RE:Stopping an Array from Taking More Values |
|
|
Wow, I never thought it would be this confusing, too bad this is the only thing holding me back from finishing the entire program. The rest of the assignment is so easy too. T_T...
Hey, md, can you explain in more detail...it just might work. It would be very helpful, please and thanks. |
|
|
|
|
|
md
|
Posted: Thu Nov 27, 2008 8:42 am Post subject: RE:Stopping an Array from Taking More Values |
|
|
You want to replace your entire for loop with the C equivalent of my pseudocode.
What it does is reads numbers from the user, and while those numbers are not the stop number, saves the number in the array, and gets another number.
I would also actually use fgets() and strtol() instead of scanf(). |
|
|
|
|
|
|
|