Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

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

  • #2
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    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};
    Correct
    Code:
    //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
    Code:
    for (x=0; x<9; x++);
    
    for (x=9;x>0; x--);
    Correct
    Code:
    for (x=0; x<10; x++)
    
    for (x=10;x>0; x--)
    Last edited by kokjj87; 05-22-2009 at 02:50 AM.

  • #3
    New to the CF scene
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by ExposedWings View Post
    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:
    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]);}
    
    }
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    New to the CF scene
    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]);}
    
    }

  • #6
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    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

  • #7
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Quote Originally Posted by kokjj87 View Post
    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.
    OracleGuy

  • #8
    New to the CF scene
    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[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]);}
    
    }

  • #9
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    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[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..
    Code:
    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-La.../dp/0131103628

    One of the best book that you can buy

    EDIT
    Code:
    for (x=10;x>0; x--)
    Should Be
    Code:
    //x start at 9 and end at 0
    for (x=9;x>=0; x--)
    Last edited by kokjj87; 05-22-2009 at 06:38 PM.

  • Users who have thanked kokjj87 for this post:

    ExposedWings (05-22-2009)

  • #10
    New to the CF scene
    Join Date
    May 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Finally! Thank you, you are an angel.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •