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, 01: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, 02: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
CorrectCode://This can only store 9 value int iArray[9]={0};2.You have a int array of the size of 10, the array numbered from iArray[0] to iArray[9], and in your code it is looping till less than 9, which is 8.. and you do not need the ; at for loopCode://This will declare a int array with a size of 10 int iArray[10];
Wrong
CorrectCode: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 02:50 AM.
05222009, 04: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, 04:35 AM
#4
 Join Date
 Sep 2002
 Location
 Saskatoon, Saskatchewan
 Posts
 16,979
 Thanks
 4
 Thanked 2,659 Times in 2,628 Posts
Kokjj isn't talking about you're printf or scanf, he's talking about you're last few lines:
Been awhile since I've seen an error caused by a loop. A for loop can legally have a semicolon at the end, which is why it compiles. But it will only execute once, and continue with the remainder of the program. The other part kokjj is talking about is that the 'magic number' 9 there is incorrect. You should be using either 10, <= / >= operators, or better yet, grabbing sizeof you're iArray since its not yet a pointer, or using a defined IARRAYSIZE constant of some sorts.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, 06: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, 05: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, 05:47 PM
#7
 Join Date
 Jun 2002
 Location
 USA
 Posts
 9,074
 Thanks
 1
 Thanked 328 Times in 324 Posts
05222009, 05: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, 06: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.
This is correct, also remember to declare both i and j variable at the top..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; } } } }
Since you are new to C, i would recommended this book for you: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
Should BeCode: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 06:38 PM.
Users who have thanked kokjj87 for this post:
ExposedWings (05222009)
05222009, 09:26 PM
#10
 Join Date
 May 2009
 Posts
 5
 Thanks
 1
 Thanked 0 Times in 0 Posts
Finally! Thank you, you are an angel.