View Full Version : C program code Help please

ExposedWings

05-22-2009, 01:54 AM

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?!?

# 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]);}

}

kokjj87

05-22-2009, 02:48 AM

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

//This can only store 9 value

int iArray[9]={0};

Correct

//This will declare a int array with a size of 10

int iArray[10];

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 loop

Wrong

for (x=0; x<9; x++);

for (x=9;x>0; x--);

Correct

for (x=0; x<10; x++)

for (x=10;x>0; x--)

ExposedWings

05-22-2009, 04:20 AM

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.

Fou-Lu

05-22-2009, 04:35 AM

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.

Kokjj isn't talking about you're printf or scanf, he's talking about you're last few lines:

//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]);}

}

Been awhile since I've seen an error caused by a loop. A for loop can legally have a semi-colon 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.

So, fix those last lines and it should fix you're program.

ExposedWings

05-22-2009, 06:25 AM

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:

# 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]);}

}

kokjj87

05-22-2009, 05:22 PM

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

oracleguy

05-22-2009, 05:47 PM

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

You're right it is, it is also going outside the bounds of the array which could be causing erroneous data to be put in the array.

ExposedWings

05-22-2009, 05:49 PM

I must just be stupid or something because that is not working either! Now I have this and it still is not working.

# 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[j-1] > iArray[j])

{

temp = iArray[j-1];

iArray[j-1] = 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]);}

}

kokjj87

05-22-2009, 06:24 PM

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.

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[j-1] > iArray[j])

{

temp = iArray[j-1];

iArray[j-1] = iArray[j];

iArray[j] = temp;

}

}

}

}

This is correct, also remember to declare both i and j variable at the top..

for (i = (10 - 1); i >= 0; i--)

{

for (j = 1; j <= i; j++)

{

if (iArray[j-1] > iArray[j])

{

iTemp = iArray[j-1];

iArray[j-1] = iArray[j];

iArray[j] = iTemp;

}

}

}

Since you are new to C, i would recommended this book for you:

http://www.amazon.com/Programming-Language-Prentice-Hall-Software/dp/0131103628

One of the best book that you can buy

EDIT

for (x=10;x>0; x--)

Should Be

//x start at 9 and end at 0

for (x=9;x>=0; x--)

ExposedWings

05-22-2009, 09:26 PM

Finally! Thank you, you are an angel.:thumbsup:

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.