More about Arrays and Functions

Mar 7, 2008 at 12:07am
I have to write a program that accepts 10 numbers from a user and stores them in an array. Pass the array and its size to a function that determines and displays the smallest and largest of the 10 values. there was also some special instructions: Make the array of numbers of type float. Note: The best way to determine the largest and smallest number in a list is to assume that the first number is the smallest and largest. Assign: large= num[0] and small = num[0]
Then use a for loop to move through the array, when a number is larger or smaller reassign large or small to that new number.

#include "stdafx.h"

#if !defined(microsoft)
#include<stdlib.h>
#define cls() system("cls");
#endif

int main()
{
const float VALUES = 10;
float intValue[10];
float intNum, a;
double total = 0;
intValue = 0.0;
void getExtremes(float);
cout<<"Please enter 10 integers."<<endl;
cout<<"Enter value 1: ";
cin>>intValue[intNum];
while (intNum < VALUES)
{
total += intValue[intNum];
++intNum;
if (intNum < VALUES)
{
cout<<"Enter the next value: ";
cin>>intValue[intNum];
}
}
cout<<"The values you entered are: ";
for (a=0;a<intNum;++a)
cout<<intValue[a]<<" ";
cout<<endl;
getche();
}
void getExtremes( float num[10])
{
float large = num[0];
float small = num[0];
for (int i=1; i<10; i++)
{
if (num[i]>large)
{
large=num[i];
}
if (num[i]<small)
{
small=num[i];
}
}
std::cout<<"n\This is the largest number: "<<large;
std::cout<<"n\This is the smallest number: "<<small;
getche();
}
Mar 7, 2008 at 12:08am
can anyone tell me what im doin wrong
Mar 9, 2008 at 1:58pm
hmmmm... i could hardly understand your code....
i think you better have it this way...
to store the numbers inputted by the user to the array
1
2
3
4
5
6
7
8
<iostream>
<conio>
main()
{
int n=10,num[11];
printf("enter 10 numbers:\n)"
for(int i=0;i<10;i++)
scanf("%d",&num[i];


then sort it from lowest to greatest


Mar 9, 2008 at 4:23pm
hey srfischer83
the variable intNum is being used as an array index and its not initialized, its always good to initialize all your variables before you use them. Primitive arrays requires constant **integer** expressions as index.
the way you initialized your array is also wrong, use braces.
C++ requires function prototypes or the definition must appear before its used, i can see the prototype inside main thats acceptable but you never used it? why?
AND the prototype says void getExtremes( float );
while the fuction actually ask for an array
void getExtremes( float num[10])
Plus you're not passing the array length to your function.
use a const variable for the length;
 
const int lengh = 10;

You need to #include "conio.h" for getch();
but if all you want is to stop the program to get that "pause" effect you can use cin.get() from <iostream> you are using it anyway for the input/output operations.
and yeah unless its #included in "stdafx.h" plz#include <iostream>

You are supposed to display the min and max value inside a ****float*** array but then you ask the user for ****integers****?
 
cout<<"Please enter 10 integers."<<endl;

fix some typos and your getExtreme function works.


more info:
http://www.cplusplus.com/doc/tutorial/functions.html
http://www.cplusplus.com/doc/tutorial/arrays.html

Jeff
Last edited on Mar 9, 2008 at 4:42pm
Topic archived. No new replies allowed.