-
Simple arrays
Hi guys I want to make a program where i get a user to enter 10 double numbers then it gets stored in a array. then the numbers are displayed back to the user. Im just wondering if i would use the standard scanf and printf commands or is there something more useful?
Code:
#include <stdio.h>
int main(void)
{
double numbers[10];
}
I know its easy but im kind of new to programming so im having some difficulties understanding. any help would be great.
-
What could possibly be more useful than scanf?
-
well i thought i was reading something earlier about read_array and show_array which is supposed to be used with arrays?
-
and would I have to loop the scanf?
-
Those were user created functions. In C, it's always a good idea to break up a program, into smaller parts (functions), for input, processing, output.
I wouldn't worry about it yet, but later, you'll want to tackle multiple functions, etc., as well.
You'll want to use scanf("%lf", &yourDoubleVariableArray[i]), for input, inside a for loop.
In pseudo code:
Code:
for 0 to 10
scanf() as shown above
print the number
//anything else you want to do
end for
-
-
so would this function correctly? sorry im not home on my comeputer with Dev c++
Code:
#include <stdio.h>
int main(void)
{
double numbers[10];
for (0 to 10)
scanf("%lf", &numbers[i]);
printf("\n" &numbers)
end for
}
-
You need to look up the for() function in your C documentation... the way you got it ain't never gonna work. That's why adak labeled it "pseudo code"... The plan was to give you the idea, but you have to write your own code.
-
for is not a function.
Typical idiom of for loop is something like:
Code:
for(i = 0; i < n ; i++) {
// do something n times
}
C Programming Tutorial
-
ok i tried something here is my code:
Code:
#include <stdio.h>
int main(void)
{
double numbers[10];
int count = 10;
printf("\nEnter the 10 numbers:\n");
int i;
for(i = 0; i < count; i ++)
{
printf("%2d> ",i+1);
scanf("%lf", &numbers[i]);
}
printf("%lf> ", &numbers[10]);
fflush(stdin);
getchar();
return 0;
}
But now I am having some trouble displaying the numbers stored in the array after.
-
Code:
printf("%lf> ", &numbers[10]);
fflush(stdin);
You need to know that printf and scanf work differently and accomplish separate things.
http://cboard.cprogramming.com/c-pro...-tutorial.html
Also, fflush(); only is guaranteed to work on output streams. If you want to "flush" stdin or an input stream you are expected to read everything. (Just as flushing an output stream writes everything.)
-
when your array is of size n, it has offset from 0 to n-1
you are printinting array[n] which has garbage data.
%d is used for integers. Double is one kind of floating value.
-
While I can agree with what you said guarav, I don't think that is related to the current problem.