Filling an Array

Filling an Array and finding the Max

Page 1 of 1

2 Replies - 3043 Views - Last Post: 02 August 2009 - 01:31 AM Rate Topic: -----

#1 JDUenas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 14-May 09

Filling an Array

Posted 02 August 2009 - 01:17 AM

I am supposed to:

Let arr be an array of 20 integers. Write a program that first fills the array with up to 20 input values and then finds and displays both the subscript of the largest item in arr and the value of the largest item.

This is what I have so far: Can someone help me out


#include <stdio.h>


void 
fill_array(int max,						/* input-declared size of arr */
		   double sentinel,				/* input-end of data value in input list */
		   double arr[],				/* output-array of data */
		   int *sizep)					/* output- number of data values stored in arr */

{
	double data;	
	int i, status;
	/* Sentinel input Loop																*/
	i=0;
	status =scanf("%lf", &data);
	while (status==1 && data != sentinel && i < max) {
		arr[i]=data;
		++ i;
		status = scanf("%lf", &data);
	}
	
	/* Issues ERROR MESSAGE																*/
	if(status != 1){
		printf("\n*** ERROR in data format***\n");
		printf("***using first %d data values***\n");
	}else if (data != sentinel){
		printf("\n***ERROR: too much data***\n");
		printf("***using first %d data values***\n");
	}
	/* Sends back size of used portion of array											*/
	*sizep=i;
}

/* Tests fill_array function															*/

#define A_SIZE 20
#define SENT -1.0

int 
main(void) 
{
	double arr[A_SIZE];
	int	use,					/* number of elements of arr in use						*/
	i; 
	
	printf("Please enter data 20 random numbers>\n");
	
	fill_array(A_SIZE, SENT, arr, &use); 
	
	printf("List of number values\n");
	for (i=0; i < use; ++i)
		printf("%13.3f\n",arr[i]);
	
	
	
	return (0);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Filling an Array

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Filling an Array

Posted 02 August 2009 - 01:31 AM

View PostJDUenas, on 2 Aug, 2009 - 07:17 AM, said:

I am supposed to:

Let arr be an array of 20 integers. Write a program that first fills the array with up to 20 input values and then finds and displays both the subscript of the largest item in arr and the value of the largest item.

This is what I have so far: Can someone help me out


#include <stdio.h>


void 
fill_array(int max,						/* input-declared size of arr */
		   double sentinel,				/* input-end of data value in input list */
		   double arr[],				/* output-array of data */
		   int *sizep)					/* output- number of data values stored in arr */

{
	double data;	
	int i, status;
	/* Sentinel input Loop																*/
	i=0;
	status =scanf("%lf", &data);
	while (status==1 && data != sentinel && i < max) {
		arr[i]=data;
		++ i;
		status = scanf("%lf", &data);
	}
	
	/* Issues ERROR MESSAGE																*/
	if(status != 1){
		printf("\n*** ERROR in data format***\n");
		printf("***using first %d data values***\n");
	}else if (data != sentinel){
		printf("\n***ERROR: too much data***\n");
		printf("***using first %d data values***\n");
	}
	/* Sends back size of used portion of array											*/
	*sizep=i;
}

/* Tests fill_array function															*/

#define A_SIZE 20
#define SENT -1.0

int 
main(void) 
{
	double arr[A_SIZE];
	int	use,					/* number of elements of arr in use						*/
	i; 
	
	printf("Please enter data 20 random numbers>\n");
	
	fill_array(A_SIZE, SENT, arr, &use); 
	
	printf("List of number values\n");
	for (i=0; i < use; ++i)
		printf("%13.3f\n",arr[i]);
	
	
	
	return (0);
}


what is your problem?
the only thing I can see is in line
		printf("***using first %d data values***\n");


where you use the conversion specification %d but don't have a paramater to print - it will either print rubbish or crash the program
Was This Post Helpful? 1
  • +
  • -

#3 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5381
  • View blog
  • Posts: 27,350
  • Joined: 10-May 07

Re: Filling an Array

Posted 02 August 2009 - 01:31 AM

Wouldn't this be a lot simpler?

#include <stdio.h>

#define MAX 20

int main(void) {
	int high=0,input[MAX],val;
	printf("Please give 20 values\n");

	for(val=0;val<MAX;val++) {
	  printf("Value #%i : ",val+1);
	  scanf("%i",&input[val]);
	  printf("\n");
	}

	printf("\n\n");
	for(val=0;val<MAX;val++) {
	  printf("Value #%i : %i\n",val+1,input[val]);
	  if(input[val]>high) high=input[val];
	}

	printf("The largest value given was %d\n",high);

	return (0);
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1