![]() |
| | #1 |
| Its hard... But im here Join Date: Apr 2005 Location: England
Posts: 1,467
| Modify an single passed array element array to a function and it modifes and returns it as expected, but when I pass an idividual array element it display it oddly then returns the wrong value but the correct element if that makes sense. Output I get is: passed array element: 816 ( where is should say 8 ) modified array element: 8 I gather my problem is possibly how I am outputing the displayed array element. I get no compile errors, its a logical error I cant faddle out but its proberly somthing really simple Code: #include <stdio.h>
#define ARRAY_SIZE 5
/*function prototypes*/
void changeArray ( int, int[] );
void changeElement ( int );
/*main function - begins program execution -----------------------------------*/
int main ( void )
{
int i, j;
/*create and display original array*/
int data[ ARRAY_SIZE ] = { 1, 2, 3, 4, 5 };
printf("Original array:\n\n");
for ( i = 0; i < ARRAY_SIZE; i++ )
{
printf("%d ", data[ i ]);
}
/*pass array size and name to function*/
changeArray ( ARRAY_SIZE, data );
/*output modified array output*/
printf("\n\nModified array\n\n");
for ( j = 0; j < ARRAY_SIZE; j++ )
{
printf("%d ", data[ j ]);
}
/*output original array element*/
printf("\n\nOrginal array element data[ 3 ]:\n\n");
printf("%d", data[ 3 ]);
/*pass element to function*/
changeElement ( data[ 3 ] );
/*output modified array element*/
printf("\n\nModified array element data[ 3 [:\n\n");
printf("%d", data[ 3 ]);
getchar(); /*freeze console output window*/
return 0; /*return value from int main*/
}
/*function to modify and return array*/
void changeArray ( int size, int x[] )
{
int i;
for ( i = 0; i < size; i++ )
{
x[ i ] *= 2;
}
}
/*function ro modify and return array element*/
void changeElement ( int e )
{
printf("%d", ( e *= 2 ));
}
__________________ I'm just trying to be a better person - My Name Is Earl |
| swgh is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,318
| Looks okay to me. The problem is that the printf() call in changeElement() does not print a new line (or alternatively, the printf() in main() before the call to changeElement() does not print a new line), so it looks like 816 when the 8 and 16 are separate.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is offline | |
| | #3 |
| Its hard... But im here Join Date: Apr 2005 Location: England
Posts: 1,467
| Thanks laserlight that solved the problem I also had one printf() call than I needed, but the output is fine now. I knew it was somthing simple. Thank you for your help
__________________ I'm just trying to be a better person - My Name Is Earl |
| swgh is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: /*function ro modify and return array element*/
void changeElement ( int e )
{
printf("%d", ( e *= 2 ));
}
-- Mats |
| matsp is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| count/display non-repeated element of array | azsquall | C++ Programming | 15 | 07-10-2008 09:42 AM |
| Exercise asks me to use a subarray. What is a subarray? | yougene | C Programming | 4 | 01-05-2008 11:30 PM |
| How to return the last element of an array? | BuezaWebDev | C Programming | 20 | 03-26-2005 10:57 AM |
| Sorting a 2-dimensional array | kmoyle73 | C++ Programming | 3 | 05-05-2004 01:54 PM |
| Merge sort please | vasanth | C Programming | 2 | 11-09-2003 12:09 PM |