Read an array of integers without a parameter

Read an array of integers without a parameter

Page 1 of 1

1 Replies - 1730 Views - Last Post: 13 February 2009 - 10:57 AM Rate Topic: -----

#1 li111g  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-February 09

Read an array of integers without a parameter

Post icon  Posted 13 February 2009 - 10:23 AM

Hello, I cannot figure out how to read an array of integers without giving an parameter, because all the numbers have to be then summed up according to even and odd. bellow is what I have

Can anyone give me a hint to how to read in first, and then I will try to work from there?

Thanks!

#include <stdio.h>
int main(void)
{
	int numbers[10000];
	int oddsum, evensum, result;
	
	printf("Enter an array of intergers:\n");
	scanf("%d", &numbers);
	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Read an array of integers without a parameter

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Read an array of integers without a parameter

Posted 13 February 2009 - 10:57 AM

This may give you a few more hints ...

Shalom,
David

/* 
	C program to find ...  the maximum, the minimum and the average values of ...
	an array of numbers entered via the keyboard; uses a DYNAMIC ARRAY of doubles
*/

#include <stdio.h>
#include <stdlib.h>

void getAryInfo(double* ary, int size, double* max, double* min, double* avg);

int main()
{
	double* ary; /* this is a pointer ... i.e. ary holds an address */
	int c, i, n;
	/* get n ... */
	do
	{
		n = 0;
		printf( "Please enter the number of observations : " );
		scanf( "%d", &n );
		while( getchar() != '\n' ); /* flush stdin ... */
		if( n <= 0 ) printf("Please enter a number greater than 0 ...\n");
	}while( n <= 0 );
	
	/* now get a block of memory for the array of n doubles */
	ary = (double*) malloc(n * sizeof(double));
	
	/*  
		ary now holds the address of the start of memory
		just allocated to hold n 'doubles' 
	*/
	
	/* now ... get the numbers into the 'ary' array memory reservd above ... */
	for( i=0; i<n; ++i )
	{
		printf( "observation %-3d : ", i+1 );
		ary[i] = 0;
		scanf( "%lf", &ary[i] ); /* Note: 'lf' for correct input of doubles */
		while( getchar() != '\n' ); /* flush all chars in stdin stream */;
		if( ary[i] == 0 )
		{
			printf("Is this value %lf correct that you entered (y/n) ? :", ary[i]);
			c = getchar();
			if( !(c == 'y' || c =='Y') )
			{
				printf("Ok ... skipped ...\n");
				--i;
				while( c != '\n' ) c=getchar(); /* flush all chars in stdin stream */;
				continue;
			}
			while( c != '\n' ) c=getchar(); /* flush all chars in stdin stream */;
		}
	}

	/* show numbers in the array ... */
	printf( "\nYou entered ...\n\n" );
	for( i=0; i<n; ++i )
	{
		printf( "%-3d : ", i+1 );
		printf( "%g\n", ary[i] );
	}
	
	/* get variables to hold info ... note: values to be returned by 'ref' */
	double max, min, avg; 
	
	/* Note: ary below is already an 'address' of the array of doubles */
	getAryInfo( ary, n, &max, &min, &avg ); /* we pass 'addresses' to pointer variables */
	
	printf( "\nThe info you desired is ... \n\n" );
	printf( "Max: %g  Min: %g  Average: %g\n\n", max, min, avg );

	printf("Press 'Enter' to continue ... ");
	getchar();
	free( ary ); /* free the dynamic memory allocated for the array of n doubles */
	return 0;
}

/* Note .... We are catching 'addresses' .... */
/*  so inside ... we have to use the value at that address by using *var   */
void getAryInfo(double* ary, int size, double* max, double* min,  double* avg)
{
	double sum = *min = *max = ary[0]; /* use this as a 'opening' value .... */
	int i;
	for( i=1; i<size; ++i ) /* start at 1 since already used 0 above */
	{
		sum += ary[i]; /* sum = sum + ary[i] */
		if (ary[i] < *min) *min = ary[i]; /* update ... if applies */
		if (ary[i] > *max) *max = ary[i];
	}  
	/* when we reach ... we have max, min and sum ... So can find average */ 
	*avg = sum/size; 
}


P.S.

I am gathering together some example problems for a free e-text on Beginning Computer Programming using C/C++... and I thought that this might make a good type of problem example/solution near the beginning?

Here is what I have 'posted' at present ... (The 2nd link is re. C/C++)

http://developers-he...index.php/topic,46.0.html
http://developers-he.../index.p...opic,106.0.html

Please take a look, if you like, and let me know what you think.

Shalom,
David

P.P.S.
I hope to put together examples that grow from easy ... to more complex ... and this problem has potential. (I like problems that can be built up in stages ... and each iteration gets more complex and adds new insights too.)

/* 
	C program to find ...  the numOdds and the numEvens for ...
	an array of numbers entered via the keyboard ...
	(This example uses a DYNAMIC ARRAY of integers.)
*/

#include <stdio.h>
#include <stdlib.h>

int getPosInteger();
void fillAry(int* a, int size);
void getAryInfo(int* a, int size, int* eSum, int* oSum);
void showOddEvenAry(int *a, int size);



int main() /* ******************** MAIN ************************************** */
{
	 /* get variables to hold odd/even info; values to be returned by 'ref' */
	int numEven, numOdd;

	int* ary; /* this is a pointer ... i.e. ary holds an address */
	int n = getPosInteger();  /* n = total number of integers in the array ...*/
	
	/* now get a block of memory for the array of n int's */
	ary = (int*) malloc(n * sizeof(int));
	
	/*  
		ary now holds the address of the start of memory
		just allocated to hold n integers
	*/
	
	/* now ... get the numbers into the 'ary' array memory reserved above ... */
	fillAry(ary, n);

	/* show numbers in the array ... */
	printf( "\nYou entered ...\n\n" );
	showOddEvenAry(ary, n);
	
	/* Note: ary below is already an 'address' of the array of int's */
	getAryInfo( ary, n, &numEven, &numOdd ); /* we pass 'addresses' to pointer variables */
	
	printf( "\nThe info you desired is ... \n\n" );
	printf( "Odd: %d   Even: %d\n\n", numOdd, numEven );

	printf("Press 'Enter' to continue ... ");
	getchar();
	free( ary );/* free the dynamic memory allocated for the array of n int's */
	return 0;
} /* ******************************** END MAIN ****************************** */



int getPosInteger()
{
	int n;
	for(;;)
	{
		n = 0;
		printf( "Please enter the number of elements : " );
		scanf( "%d", &n );
		while( getchar() != '\n' ); /* flush stdin ... */
		if( n <= 0 )
		{
			printf("Please enter a number greater than 0 ...\n");
			continue;
		}
		// else ... if reach here
		return n;
	}
}

void fillAry(int* a, int size)
{
	int i, c;
	for( i=0; i<size; ++i )
	{
		printf( "element %-3d  :  ", i+1 );
		a[i] = 0;
		scanf( "%d", &a[i] ); /* Note: 'lf' for correct input of int's */
		while( getchar() != '\n' ); /* flush all chars in stdin stream */;
		if( a[i] == 0 )
		{
			printf("Is this value %d correct that you entered (y/n) ? :", a[i]);
			c = getchar();
			if( !(c == 'y' || c =='Y') )
			{
				printf("Ok ... skipped ...\n");
				--i;
				while( c != '\n' ) c=getchar(); /* flush all chars in stdin stream */;
				continue;
			}
			while( c != '\n' ) c=getchar(); /* flush all chars in stdin stream */;
		}
	}
}

/* Note .... We are catching 'addresses' .... */
/*  so inside ... we have to use the value at that address by using *var   */
void getAryInfo(int* a, int size, int* eSum, int* oSum)
{
	int i;
	*eSum = 0;
	*oSum = 0;
	for( i=0; i<size; ++i ) /* start at 1 since already used 0 above */
	{
		if (a[i]%2 == 0) ++*eSum;
		else ++*oSum;
	}  
}

void showOddEvenAry(int *a, int size)
{
	int i;
	for(i=0; i<size; ++i)
	{
		printf( "%-3d : ", i+1 );
		if( a[i] % 2  == 0 ) printf( "\t\t%d\n", a[i] );
		else printf( "%d\n", a[i] );
	}
}

This post has been edited by David W: 13 February 2009 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1