![]() |
| | #1 |
| Registered User Join Date: Apr 2009 Location: jacksonville fl
Posts: 7
| array issues 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 | |
| | #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 | |
| | #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 | |
| | #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 | |
| | #5 | |
| Registered User Join Date: Sep 2006
Posts: 2,512
| Quote:
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.
| |
| Adak is offline | |
| | #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 | |
| | #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 | |
| | #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;
}
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
}
|
| Adak is offline | |
| | #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 | |
| | #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 | |
![]() |
| Tags |
| /code, code |
| Thread Tools | |
| Display Modes | |
|
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 |