C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-25-2009, 09:08 AM   #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;
}
Rob123 is offline   Reply With Quote
Old 04-25-2009, 09:27 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 04-25-2009, 12:19 PM   #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 12:23 PM.
Rob123 is offline   Reply With Quote
Old 04-25-2009, 12:22 PM   #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.
Rob123 is offline   Reply With Quote
Old 04-25-2009, 01:28 PM   #5
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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.
Adak is offline   Reply With Quote
Old 04-25-2009, 01:37 PM   #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.
Rob123 is offline   Reply With Quote
Old 04-25-2009, 02:16 PM   #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;
}
Rob123 is offline   Reply With Quote
Old 04-25-2009, 06:26 PM   #8
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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.
Adak is offline   Reply With Quote
Old 04-26-2009, 08:31 AM   #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++?
Rob123 is offline   Reply With Quote
Old 04-26-2009, 09:35 AM   #10
Registered User
 
Join Date: Sep 2006
Posts: 2,512
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.
Adak is offline   Reply With Quote
Reply

Tags
/code, code

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 08:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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