GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2004, 16:13
justachessgame justachessgame is offline
New Member
 
Join Date: Oct 2004
Posts: 13
justachessgame is on a distinguished road

float arrays question


I am having problems writing this code. I have to write a program that declares a float array with 100 elements and also:

1. displays the sum of the elements in the array
2. displays the smallest number in the array
3. displays the 5 largest numbers in the array

If anyone could provide me with a little (or a lot) of help I would appreciate it! Please...

This is the beginning that I have so far...

CPP / C++ / C Code:
#include <stdio.h>

int x = 100;
int main()
{
	x = 100;
	printf( "x = %d\n", x );
	float x[100];
	
	return 0;
}
  #2  
Old 08-Nov-2004, 17:20
justachessgame justachessgame is offline
New Member
 
Join Date: Oct 2004
Posts: 13
justachessgame is on a distinguished road
OK...I have created a program but it does not do what I want it to do..I am only able to find the greatest of three numbers...how do I get five?

Here is what I have since my last post...

CPP / C++ / C Code:
#include <stdio.h>

int main()
{
    int x, y, z, max;
    
	
    printf ( "Enter a number " ); //Evaluates first number
    scanf ( "%d" , &x);
	printf ( "Enter a number " ); //Evaluates second number
    scanf ( "%d" , &y);
	printf ( "Enter a number " ); //Evaluates third number
    scanf ( "%d" , &z);

    if( x > y )
		max = x;
	else
		max = y;
	if( x < z )
		max = z;
	
	
		
	printf( "The greatest value is %d", max ); //Finds the greatest number of three not five like I need.
	
	  
    return (0);
  #3  
Old 08-Nov-2004, 18:06
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by justachessgame
I am having problems writing this code. I have to write a program that declares a float array with 100 elements and also:

1. displays the sum of the elements in the array
2. displays the smallest number in the array
3. displays the 5 largest numbers in the array

If anyone could provide me with a little (or a lot) of help I would appreciate it! Please...

This is the beginning that I have so far...

CPP / C++ / C Code:
#include <stdio.h>

int x = 100;
int main()
{
	x = 100;
	printf( "x = %d\n", x );
	float x[100];
	
	return 0;
}


I don't understand your declarations for int x. You want float x[100].

I'll get you started:

First of all in order to test the code, you have to put something into the array.

I will fill the array with random numbers between 0 and 1. Maybe you want some other function to put some values in the array, since with random values, how will you know you have the right answer? For example you could simply set ary[i] = (float)i in your fill_array() function. Whatever.

Here's a thought: start with an array, say of 10 elements and initialize it with some specific values. Then print out all of the values, and see if your functions are working properly on them. Just a thought.

Here's a start:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  void fill_array(float *, int);
  float sum_array(float *, int);
  float smallest_in_array(float *, int);
  void  find_smallest_five(float *, float *);
  float x[100];

  fill_array(x, 100);
  
  return 0;
}

void fill_array(float *ary, int size)
{
  int i;
  for (i = 0; i < size; i++) {
    ary[i] = (float)rand()/(float)RAND_MAX;
  }
}

Now, you must make functions that step through the array that

1. add the elements and return the value in function sum_array().

2. find the smallest element in the array and return its value in function smallest_in_array().

3. find the smallest five elements and return them somehow. Do the first two things and save this for last.

Regards,

Dave
  #4  
Old 08-Nov-2004, 18:12
justachessgame justachessgame is offline
New Member
 
Join Date: Oct 2004
Posts: 13
justachessgame is on a distinguished road
Quote:
Originally Posted by davekw7x
I don't understand your declarations for int x. You want float x[100].

I'll get you started:

First of all in order to test the code, you have to put something into the array.

I will fill the array with random numbers between 0 and 1. Maybe you want some other function to put some values in the array, since with random values, how will you know you have the right answer? For example you could simply set ary[i] = (float)i in your fill_array() function. Whatever.

Here's a thought: start with an array, say of 10 elements and initialize it with some specific values. Then print out all of the values, and see if your functions are working properly on them. Just a thought.

Here's a start:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  void fill_array(float *, int);
  float sum_array(float *, int);
  float smallest_in_array(float *, int);
  void  find_smallest_five(float *, float *);
  float x[100];

  fill_array(x, 100);
  
  return 0;
}

void fill_array(float *ary, int size)
{
  int i;
  for (i = 0; i < size; i++) {
    ary[i] = (float)rand()/(float)RAND_MAX;
  }
}

Now, you must make functions that step through the array that

1. add the elements and return the value in function sum_array().

2. find the smallest element in the array and return its value in function smallest_in_array().

3. find the smallest five elements and return them somehow. Do the first two things and save this for last.

Regards,

Dave

I see what you are saying Dave...but I really don't understand how to get 100 or even 10 elements to output randomly. I know that I have to use srand but how do I format it? By the way...thank you for replying to my message.
  #5  
Old 08-Nov-2004, 18:17
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by justachessgame
I see what you are saying Dave...but I really don't understand how to get 100 or even 10 elements to output randomly. I know that I have to use srand but how do I format it? By the way...thank you for replying to my message.
OK. Here's a test case with 10 elements:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>


int main()
{
  void fill_array(float *, int);
  float sum_array(float *, int);
  float smallest_in_array(float *, int);
  void  find_smallest_five(float *, float *);
  float x[10];

  int i;

  fill_array(x, 10);

  for (i = 0; i < 10; i++) {
    printf("x[%d] = %f\n", i, x[i]);
  }
  
  return 0;
}

void fill_array(float *ary, int size)
{
  int i;
  for (i = 0; i < size; i++) {
    ary[i] = (float)rand()/(float)RAND_MAX;
  }
}
A note: I filled the array by calling rand() a number of times. Since I didn't use srand() to seed the random number generator, you will get the same numbers every time you run the program. That makes it easier to debug (at least it does for me). If you would rather initialize the array with specific numbers, then throw away everything associated with fill_array(), change this in main:

change this
CPP / C++ / C Code:
float x[10];

to something like this
CPP / C++ / C Code:
float x[10] = {1.2, 33.0, -99.0, 0.123, -1.222, 5.0, 89.999, 23.0, 0.0, 622441.0};



Now, it's your turn.

Regards,

Dave
  #6  
Old 08-Nov-2004, 18:18
justachessgame justachessgame is offline
New Member
 
Join Date: Oct 2004
Posts: 13
justachessgame is on a distinguished road
OK...this generates 100 random numbers...but chooses the second largest. How do I make it choose the 5 largest?
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
	int s, t;
	int max;
	int max2;
	int x[100];
	int temp;
	
	// Create and initialize array
	srand( time( NULL ) ); // seed random number generator

	for( t = 0; t < 100; t = t + 1 )
	{
		x[t] = rand() % 100;
	}
	
	//check for the max
	max = x[0] ;
	for( t = 1; t < 100; t++ )
	{
	   if( x[t] >= max )
	   {
  	  	  max = x[t];
   	   }
    }

	//check for max2
	max2 = x[0] ;
	for( t = 1; t < 100; t++ )
	{
	   if( x[t] >= max2 && x[t] < max )
	   {
  	  	  max2 = x[t];
   	   }
    }

	// display array contents
	for( t = 0; t < 100; t++ )
	{
	   printf( "%d ", x[t] ); 	  
	}
	
	//display current max
    printf ( "\n" ) ;
	printf ( "max = %d\n", max );
		
	
	printf( "The second largest number is %d\n", max2 );

    
	return 0;
}

  #7  
Old 08-Nov-2004, 20:37
nkhambal nkhambal is offline
Regular Member
 
Join Date: Jul 2004
Location: CA USA
Posts: 315
nkhambal is a jewel in the roughnkhambal is a jewel in the rough
Following is one way to find 5 largest numbers in set of 100 numbers.

CPP / C++ / C Code:
int mx1,mx2,mx3,mx4,mx5;      //5 variables to hold 5 max numbers

mx1=mx2=mx3=mx4=mx5=0;	//Initialize all maximum numbers to 0.

  for( t =0; t < 100; t++)
  {
			if (!(x[t]==mx1) && !(x[t]==mx2) && !(x[t]==mx3) && !(x[t]==mx4) && !(x[t]==mx5)) //Avoid Duplicate numbers
			{
			
				if(x[t]>mx1)
				 {
					 mx5=mx4;
					 mx4=mx3;
					 mx3=mx2;
					 mx2=mx1;
					 mx1=x[t];
				 } else if (x[t]>mx2)
				 {
					mx5=mx4;
					mx4=mx3;
					mx3=mx2;
					mx2=x[t];
				 } else if (x[t]>mx3)
				 {
					mx5=mx4;
					mx4=mx3;
					mx3=x[t];
				 } else if (x[t]>mx4)
				 {
					 mx5=mx4;
					 mx4=x[t];
				 } else if (x[t]>mx5)
				 {
					mx5=x[t];
				 }
			 }
}

printf ( "\n5 Maximum numbers are\n" ) ;
  printf ( "mx1:= %d  mx2:= %d  mx3:=%d  mx4:=%d  mx5:=%d \n", mx1,mx2,mx3,mx4,mx5 );
    

This is just one way. Someone might come up with more efficient way to do it.
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I was wondering.... n3pla Computer Software Forum - Windows 1 28-Jul-2002 04:33

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:44.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.