I have to find the largest number by using those function, but the largest number doesn't appear when i enter the 8 numbers

#include <iostream>
using namespace std;
double max (double array[], int size);
int main ()
{
double large[8],x=8, maximum;
int num;

cout << "Enter eight numbers, one at a time, and the program will return the largest number. \n";
cout << "Enter first number : ";
cin >> large[8];

for (int i=1; i<x; i++)
{
cout << "Enter another number :";
cin >> num;
maximum = max(large,num);
}
cout << "\nThe largest number is " << maximum;
return 0;
}
double max (double array[], int size)
{
double x;
x = array[0];
int y;
if (x>y)
return x;
else
return y;
}
Last edited on
your code, indented
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;
double max(double array[], int size);
int main() {
	double large[8], x = 8, maximum;
	int num;

	cout << "Enter eight numbers, one at a time, and the program will return "
	        "the largest number. \n";
	cout << "Enter first number : ";
	cin >> large[8]; //out of bounds

	for(int i = 1; i < x; i++) {
		cout << "Enter another number :";
		cin >> num; //so we read in `num', ¿what was the purpose of the array `large'?
		maximum = max(large, num); //max(array = large, size = num)  ¿`num' represents a size?
	}
	cout << "\nThe largest number is " << maximum;
	return 0;
}
double max(double array[], int size) {
//size is unused.
	double x;
	x = array[0]; //you asked for an array but just use one element (which comes uninitialised from main)
	int y; //uninitialised
	if(x > y) //using an uninitialised variable: undefined behaviour
		return x;
	else
		return y; //returning an uninitialised variable
}
Last edited on
Topic archived. No new replies allowed.