Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 10 of 10
Thread: C program code Help please

05222009, 02:54 AM #1
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
C program code Help please
I am writing this program that is supposed to allow the user to enter 10 numbers into an array and then give the option to display the number sin ascendign or descending order. I do not know what I am doing wrong. The program compiles just fine with no errors but after running the program the only display I get is the last value that was entered by the user. What am I doing wrong?!?
Code:# include <stdio.h> main() { int x=0; int iArray[9]={0}; int iResponse=0; int iTemp=0; //Prompt for user input for ( x= 0; x< 10; x++){ printf("\nEnter a number:\n"); scanf("%d", &iArray[x]);} //menu for order printf("\nEnter your selection\n"); printf("\n1\tList your numbers in ascending order\n"); printf("\n2\tList your numbers in descending order\n"); scanf("%d", &iResponse); //Sort numbers for( x=0; x<10; x++) { for( x=0; x<10; x++) { if(iArray[x]>iArray[x+1]) { iTemp = iArray[x+1]; iArray[x+1] = iArray[x]; iArray[x] = iTemp; } } } //Display order if (iResponse == 1){ for (x=0; x<9; x++); printf("\n %d\n",iArray[x]);} if (iResponse == 2){ for (x=9;x>0; x); printf("\n%d\n", iArray[x]);} }

05222009, 03:48 AM #2
Welcome to the coding forums!
Here are the mistake that i have spot in your code.
1.You are going to store 10 int in your array, so you must declare a int array that can store 10 int value..
Wrong
Code://This can only store 9 value int iArray[9]={0};
Code://This will declare a int array with a size of 10 int iArray[10];
Wrong
Code:for (x=0; x<9; x++); for (x=9;x>0; x);
Code:for (x=0; x<10; x++) for (x=10;x>0; x)
Last edited by kokjj87; 05222009 at 03:50 AM.

05222009, 05:20 AM #3
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
I tried all of that and it still does not work. It is supposed to display the numbers entered in either ascending or descending order. And you always have to put the ; at the end of a printf() or scanf() or else you get a parse error.

05222009, 05:35 AM #4
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 17,026
 Thanks
 4
 Thanked 2,668 Times in 2,637 Posts
Kokjj isn't talking about you're printf or scanf, he's talking about you're last few lines:
Code://Display order if (iResponse == 1){ for (x=0; x<9; x++); printf("\n %d\n",iArray[x]);} if (iResponse == 2){ for (x=9;x>0; x); printf("\n%d\n", iArray[x]);} }
So, fix those last lines and it should fix you're program.PHP Code:header('HTTP/1.1 420 Enhance Your Calm');

05222009, 07:25 AM #5
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
Ok that sort of fixed the problem, thank you, but I think there is still an error in the sort because it is still not sorting the values in order. I have went ovet this code a million times and can not figure out what I am doing wrong. Second if I choose the second option in the menu, to descend the numbers, for some reason it always displays 2280632 as the first value, and I have no clue where it gets that number from. So here is what I have now:
Code:# include <stdio.h> main() { int x=0; int iArray[10]={0}; int iResponse=0; int iTemp=0; //Prompt for user input for ( x= 0; x< 10; x++){ printf("\nEnter a number:\n"); scanf("%d", &iArray[x]);} //menu for order printf("\nEnter your selection\n"); printf("\n1\tList your numbers in ascending order\n"); printf("\n2\tList your numbers in descending order\n"); scanf("%d", &iResponse); //Sort numbers for( x=0; x<10; x++) { for( x=0; x<10; x++) { if(iArray[x]>iArray[x+1]) { iTemp = iArray[x+1]; iArray[x+1] = iArray[x]; iArray[x] = iTemp; } } } //Display order if (iResponse == 1){ for (x=0; x<10; x++) printf("\n %d\n",iArray[x]);} if (iResponse == 2){ for (x=10;x>0; x) printf("\n%d\n", iArray[x]);} }

05222009, 06:22 PM #6
I think that the bubble sorting algorithm that you are trying to implement is wrong..
take a look here..
http://www.algorithmist.com/index.php/Bubble_sort.c

05222009, 06:47 PM #7
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,073
 Thanks
 1
 Thanked 328 Times in 324 Posts

05222009, 06:49 PM #8
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
I must just be stupid or something because that is not working either! Now I have this and it still is not working.
Code:# include <stdio.h> main() { int x=0; int iArray[10]={0}; int iResponse=0; int iTemp=0; //Prompt for user input for ( x= 0; x< 10; x++){ printf("\nEnter a number:\n"); scanf("%d", &iArray[x]);} //menu for order printf("\nEnter your selection\n"); printf("\n1\tList your numbers in ascending order\n"); printf("\n2\tList your numbers in descending order\n"); scanf("%d", &iResponse); //Sort numbers void bubbleSort(int iArray[], int array_size) { int i, j, temp; for (i = (10  1); i >= 0; i) { for (j = 1; j <= i; j++) { if (iArray[j1] > iArray[j]) { temp = iArray[j1]; iArray[j1] = iArray[j]; iArray[j] = temp; } } } } //Display order if (iResponse == 1){ for (x=0; x<10; x++) printf("\n %d\n",iArray[x]);} if (iResponse == 2){ for (x=10;x>0; x) printf("\n%d\n", iArray[x]);} }

05222009, 07:24 PM #9
You are not suppose to put this whole chunk of code in to yours just like that, this is a function, not a normal procedure.
Code:void bubbleSort(int iArray[], int array_size) { int i, j, temp; for (i = (10  1); i >= 0; i) { for (j = 1; j <= i; j++) { if (iArray[j1] > iArray[j]) { temp = iArray[j1]; iArray[j1] = iArray[j]; iArray[j] = temp; } } } }
Code:for (i = (10  1); i >= 0; i) { for (j = 1; j <= i; j++) { if (iArray[j1] > iArray[j]) { iTemp = iArray[j1]; iArray[j1] = iArray[j]; iArray[j] = iTemp; } } }
http://www.amazon.com/ProgrammingLa.../dp/0131103628
One of the best book that you can buy
EDIT
Code:for (x=10;x>0; x)
Code://x start at 9 and end at 0 for (x=9;x>=0; x)
Last edited by kokjj87; 05222009 at 07:38 PM.

Users who have thanked kokjj87 for this post:
ExposedWings (05222009)

05222009, 10:26 PM #10
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
Finally! Thank you, you are an angel.