hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's not allowed to change the array. loops allowed-maximum 3!
thanks,
hello, i need a function which gets integers array, and prints it in ascending order, the point is that it's not allowed to change the array. loops allowed-maximum 3!
thanks,
Welcome aboard.
Please read the three sticky notes (Announcements).
The first two:
C Board - Announcements in Forum : C Programming
C Board - Announcements in Forum : General Programming Boards
Tim S.
Oh, I am sorry would you like champagne while you wait as well sir?
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
What did your instructor suggest in class, that you use to complete this assignment?
Show what you've done, and tell us what the instructor recommended for this problem. That will save a lot of time, and get you off to a good start.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
I've got a bet going on how the student winds up doing this.
Money, money, money, money -- $money$!
It's a sing-along with Pink Floyd! < ROFL >
well, i thank all nice people for being such nice,
any way, i am a new user on this forum,
i wrote
the problem is that i can't find a way to complete the function, i know that i should have another loop that compares other numbers and find next minimum, can i get some ideas??Code:{ int i,x; int min,tmp; int A[SIZE]; for(i=0;i<SIZE;i++) { printf("Please insert to array grade to idx %d\n",i); fflush(stdin); scanf("%d",&A[i]); } min=A[0]; for(x = 0; x < SIZE; x++) { if(A[x]<min ) min=A[x]; } printf("%d\n",min); }
thanx
You may need to use more parameters, but that's not prohibited by the instructor. Any variable that you want to change, in a local function, and have it stayed changed after the function returns, needs to have an & "address of" operator used in the calling function, and a * used in the receiving function:Code:{ int i,x; int min,tmp; int A[SIZE]; //add int max variable //add int variable sum equals 0, here for(i=0;i<SIZE;i++) { printf("Please insert to array grade to idx %d\n",i); fflush(stdin); scanf("%d",&A[i]); getchar(); //removes left over newline char from the enter key //} if(i equals zero) { //assign min to A[i] //assign max to A[i] } if(A[i] is less than min) //what is min now equal to? if(A[i] is greater than max) //what is max now equal to? sum equals sum plus A[i]; //get your sum going } //for(x = 0; x < SIZE; x++) //{ //if(A[x]<min ) min=A[x]; //} printf("%d\n",min); }
If I just wrote:Code:getData(A, &min, &max, &sum); //<== the call to the function void getData(int A[50], int *min, int *max, int *sum) { //<==the function receiving it //etc. }
then any changes made to min, max, or sum, would be lost when control returned from getData, since here, all these variables are not addresses, they're just copies being sent to getData().Code:getData(A, min, max, sum);
Last edited by Adak; 11-09-2010 at 10:08 AM.
Also, never fflush(stdin).
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.