C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-03-2007, 10:39 AM   #1
Its hard... But im here
 
swgh's Avatar
 
Join Date: Apr 2005
Location: England
Posts: 1,467
Modify an single passed array element

Title basicly says what wont work. Half of the below works, I pass the whole
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   Reply With Quote
Old 08-03-2007, 10:47 AM   #2
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 08-03-2007, 10:54 AM   #3
Its hard... But im here
 
swgh's Avatar
 
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   Reply With Quote
Old 08-04-2007, 08:58 AM   #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 ));
}
This function doesn't modify anything, right? It's got a local parameter that gets modified locally, but the value that was passed in doesn't get changed.

--
Mats
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:09 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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