Join 137,374 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,998 people online right now. Registration is fast and FREE... Join Now!
okI'm starting out small so please bear with me. This is what I have to do. C program that will accept up to 50 validated integers from the keyboard (a null entry terminates the input process), store the integers in a subscripted array, sort the array in ascending sequence, copy the sorted values to a linked list organized with pointers, and then display the values on the screen. so i have broken it into 6 step. So the first on i want to do is to input the intergerts into an array. I have lowed the number in the array to 5. So i need to know what i did wrong to get it into my array the first time
CODE
#include <stdio.h>
#define Max 5 //number to set array
main () { int Num[Max]; // numbers to be added to array
printf ("please enter 5 random nunber"); scanf ("%d", &Num);
while (Num == NULL) { printf ("please enter 50 random nunber"); scanf ("%d", Num); } printf ("%d", Num);
As maximus said, you need to use a loop to actually move positions within the array. The way you have it now, it is assigning only one element of the array. With the For loop, 'i' acts as both a counter as well as the place in the array.
As the loop runs, 'i' is increased by one ("i++") each time. when you scan in the number inside the loop you assign it to "num[i]". Say 'i' = 2, so when you run the loop, you will assign to "num[2]", which is actually the third place in an array. Array positions start with 0 being the first place.
One thing he didn't mention was that within the "while" loop you have there, on your scanf statement, you are missing a & before "num"
[where can i find information on subscripted array. I searched a couple of sites last night and in my bood and did not find anything
The subscript operator is the [] operator...it simply refers to indexing an array.
so my next strp is going to be sort the array in ascending sequence. So i just need to write a funcation that will sort the numbers in ascending order.
ok this is what I have come up with to do the progam listed above. Im getting the following error when i try to debug any ideas on what i did wrong. E:\tmitchellwk3.cpp In function `int main()': 58 E:\tmitchellwk3.cpp invalid conversion from `int' to `int*' 58 E:\tmitchellwk3.cpp initializing argument 1 of `void ArraySort(int*, int (*)(int, int), unsigned int)'
CODE
#include <stdio.h> #include <stdlib.h>
#define unit32 unsigned int #define Max 5 //number to set array
int cmpfun (int a, int b) { if (a > b) return 1; else if (a < b) return -1; else return 0; } main () { int Num[Max]; // numbers to be added to array int i;
for (i = 0; i < Max && Num[i] != 0; i++) { printf ("Please enter a random nunber : "); scanf ("%d", &Num[i]); }
ArraySort(i, cmpfun, Max);
for (i = 0; i < Max; i ++) printf ("%d\n", Num[i]);
int cmpfun (int a, int b) { if (a > b) return 1; else if (a < b) return -1; else return 0; } int main () { int Num[Max]; /* numbers to be added to array*/ int i;
for (i = 0; i < Max && Num[i] != 0; i++) { printf ("Please enter a random nunber : "); scanf ("%d", &Num[i]); }
ArraySort(Num, cmpfun, Max);
for (i = 0; i < Max; i ++) printf ("%d\n", Num[i]);
getchar(); getchar(); return 0; }
This post has been edited by Xing: 22 Sep, 2006 - 03:48 PM