array issues

This is a discussion on array issues within the C Programming forums, part of the General Programming Boards category; I am having issues with getting input numbers from keyboard and putting them into an array and displaying them in ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Location
    jacksonville fl
    Posts
    7

    array issues

    I am having issues with getting input numbers from keyboard and putting them into an array and displaying them in original order, ascending order, and descending order. I am stuck on getting the input from the keyboard. Is my scanf/printf statement incorrect?

    Code:
    #include <stdafx.h>
    #include <stdio.h>
    #include <ctype.h>
    #define MAX_SIZE 5
    
    int main (void)
    {
    	//int nums[5] = {1, 2, 3, 4, 5};
    	int nums[5];
    	int i;
    
    //Statements
    printf ("Enter 5 numbers\n");
    for (i = 0; i < 5; i++)
    	scanf ("%d", &nums[5]);
    	printf ("%d, %d, %d, %d, %d", nums[5]);
    
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your scanf statement is asking for ONE number, and you are telling the compiler you want it stored in the 6th element of the array. Then you tell printf to print 5 numbers and pass ONE element (again, the 6th element).

    Since your actual array has 5 elements both printf and scanf will access outside of the valid range of the array.

    Your indentation also indicates that you want the printf to occur 5 times, but as it stands, it will only be executed once you have input 5 numbers into the one position in the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Location
    jacksonville fl
    Posts
    7

    array issues

    Thanks Mats,

    printf ("Enter 5 numbers\n");
    for (i = 0; i < 5; i++)
    scanf ("%d %d %d %d %d", &nums[5]);
    printf ("%d, %d, %d, %d, %d", nums[5]);

    Would this be a better scanf? what about the &nums[5]? Is this the part telling it to place the number in the 6th digit? It is a little confusing to me. I appreciate the reply.
    Last edited by Rob123; 04-25-2009 at 01:23 PM.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Location
    jacksonville fl
    Posts
    7

    array issues

    I am new to this and am trying to create this for work. The overall project is to have the user input 5 numbers and display them in the original order, ascending order, and descending order. They will be reference to other attributes, but that is another techs piece to build. I am assuming I will need 1 array with two prointers for the ascending and descending order? Thanks.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Rob123 View Post
    I am new to this and am trying to create this for work. The overall project is to have the user input 5 numbers and display them in the original order, ascending order, and descending order. They will be reference to other attributes, but that is another techs piece to build. I am assuming I will need 1 array with two prointers for the ascending and descending order? Thanks.
    Code:
    int A[5] = {27, 12, 2, 44, 35};
    
    /*idx array must be initialized in order, either this way, or (more
    commonly), through a loop.
    */
    int idx[5] = {0,1,2,3,4,};           //A=main Array, idx= index int
    
    
    /* Original Order*/
    for(i = 0; i < 5; i++)
       printf("\n %d", A[i]
    
    /* Ascending order */
    for(i = 0; i < 5 - 1; i++)  {
       for(j = i + 1; j < 5; j++)  {
          if(A[i] > A[j]) {
             temp = A[idx[i]];
             A[idx[i]] = A[idx[j]];
             A[idx[j]] = temp;
          }
       }
    }         
    
    /* print the ascending order */
    for(i = ....)
       printf("\n %d, A[idx[i]]);
    
    For descending order, change the if(... >...) to if(...<...)
    
    and print it just like the ascending order.
    You could use a more sophisticated algorithm, of course. Pointers, structs, whatever.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    jacksonville fl
    Posts
    7

    array issue

    Adak,
    Would I use a scanf statement for getting the user input? Thank you very much for the reply. It helps a great deal. Being new to this it amazes me on how much knowledge people on this board have. I see some of the code in here and I wonder if I will ever get there.Will this work with user input from keyboard? It looks as though the numbers are assigned.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Location
    jacksonville fl
    Posts
    7
    This is my current code. The original numbers are displaying fine. The ascending numbers are not. Only four of the numbers are being displayed and the number 12 is appearing twice. Any advice?

    Code:
    #include "stdafx.h"
    
    int main (void)
    {
    int A[5] = {27, 12, 2, 44, 35};
    
    /*idx array must be initialized in order, either this way, or (more
    commonly), through a loop.
    */
    int idx[5] = {0,1,2,3,4,};           //A=main Array, idx= index int
    int j;
    int i;
    int temp;
    
    /* Original Order*/
    printf("\nAscending\tOriginal\tDescending\n");
    for(i = 0; i < 5; i++)
       printf("\t\t %d\n", A[i]);
    
    /* Ascending order */
    for(i = 0; i < 5 - 1; i++)  {
       for(j = i + 1; j < 5; j++)  {
          if(A[i] > A[j]) {
             temp = A[idx[i]];
             A[idx[i]] = A[idx[j]];
             A[idx[j]] = temp;
    		 printf("%d\n", A[idx[i]]);
          }
       }
    }
    /* Descending order */
    //for(i = 0; i < 5 - 1; i++)  {
       //for(j = i + 1; j < 5; j++)  {
          //if(A[i] < A[j]) {
             //temp = A[idx[i]];
             //A[idx[i]] = A[idx[j]];
             //A[idx[j]] = temp;
    		 //printf("\t\t\t\t%d\n", A[idx[i]]);
          //}
       //}
    //}         
    
    /* print the ascending order */
    //for(i = ....)
       //printf("\n %d", A[idx[i]]);
    
    //For descending order, change the if(... >...) to if(...<...)
    
    //and print it just like the ascending order.
    return 0;
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't feel alone. I feel in awe myself, when I see some replies from the pro's.

    ]This is my current code. The original numbers are displaying fine. The ascending numbers are not. Only four of the numbers are being displayed and the number 12 is appearing twice. Any advice?


    Oh! this is C++!! That might change things.
    Looks like it might be OK, but that's a dodgy practice.

    Code:
    #include "stdafx.h"
    
    int main (void)
    {
    /*You may want to set the A array to all zero's to start with */
    int A[[] = { 0 };
    
    /* instead of these assignments to the array */
    int A[5] = {27, 12, 2, 44, 35};
    
    /*idx array must be initialized in order, either this way, or (more
    commonly), through a loop.
    */
    int idx[5] = {0,1,2,3,4,};           //A=main Array, idx= index int
    int j;
    int i;
    int temp;
    
    /* Original Order*/
    printf("\nAscending\tOriginal\tDescending\n");
    for(i = 0; i < 5; i++)
       printf("\t\t %d\n", A[i]);
    
    /*
    This is the problem - you have combined the sorting, with the display. 
    The code looks fine, but you have to do the sorting of the index
    *first*, and *then* do the display.
    */
    
    /* Ascending order */
    for(i = 0; i < 5 - 1; i++)  {
       for(j = i + 1; j < 5; j++)  {
          if(A[i] > A[j]) {
             temp = A[idx[i]];
             A[idx[i]] = A[idx[j]];
             A[idx[j]] = temp;
    /* ==========> none of this yet: printf("%d\n", A[idx[i]]); */
          }
       }
    }
    
    /*NOW print the ascending order */
    for(i = 0; i < 5; i++)
       printf("\n %d", A[idx[i]]);
    
    /*
    You have to print the ascending order, before you do the resort
    for descending order, because we're re-using the same indexes.
    */
    
    /* Descending order */
    for(i = 0; i < 5 - 1; i++)  {
       for(j = i + 1; j < 5; j++)  {
          if(A[i] < A[j]) {
             temp = A[idx[i]];
             A[idx[i]] = A[idx[j]];
             A[idx[j]] = temp;
    /* Delete this line=====>printf("\t\t\t\t%d\n", A[idx[i]]); */
          }
       }
    }         
    
    
    //For descending order, change this:
          if(A[i] < A[j]) {
    
    to
    
          if(A[i] > A[j] { 
    
    //and print it just like the ascending order.
    return 0;
    }
    Yes, you can enter the integers any way you want - scanf() is fine for careful use, but scanf() won't handle wrong input well in the real world.

    Usual loop might be something like:
    Code:
    for(i = 0; i < 5; i++)  {
       printf("Enter number %d: ", i);
       scanf("%d", &A[i]);
       printf("Enter another number (y/n)? ");
       scanf(" %c", &yesno);
       
       if(yesno == 'n' || yesno == 'N')
         break;
       yesno = getchar();  //pull the newline char off the keyboard buffer
    }
    I haven't run any of this code, so it might have a bug in it - but it will be very close.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Location
    jacksonville fl
    Posts
    7

    array issue

    Adak,

    Actually it is just plain c. That is what is supposed to be anyway. I am still trying to get a handle on the basics. I bought two books, but it doesn't go into detail about getting input from keyboard and putting them into arrays. They do go into detail about static numbers into arrays though. Thanks for your help on this. I appreciate it. Is the last code you sent me in c, or c++?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    My code is only in C. It might run in C++, but I don't program in C++.

    And that code snippet has not been tested/run, at all yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 01:56 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 01:13 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21