making a array

create an array of random numbers (7-14)
(not sorted)

use a for loop and find both the largest and smallest elements (integers).

hint:initialize the maxnum and minnum with the value of the first element in the array.

if ( maxnum < elelmentofarray)
{
// then update maxnum
}

i know how to make a array just need help with the rest
C++ already has algorithms to do this (you could use them to impress your professor).
http://www.cplusplus.com/reference/algorithm/min_element/
http://www.cplusplus.com/reference/algorithm/max_element/

Anyway, what you need to do is store the maximum element in maxnum, and minimum element in minnum.

1
2
3
4
5
int maxnum = array[0];

for (int i=0; i < size_of_array; ++i)
    if (maxnum < array[i])
        maxnum = array[i];

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
#include<iostream>
#include<cstdlib>
using namespace std;


int main()
{
	int random[5];
	int biggest = 0;
	int smallest = 100;

for(int i=0;i<5;i++)
{
random[i] = (rand()%7)+7;
if(random[i]>biggest)
	biggest=random[i];
if(random[i]<smallest)
	smallest=random[i];
}

for(int x=0;x<5;x++){
	cout<<"Index "<<x<<" - - "<<random[x]<<endl;
}

cout<<"The biggest value of the array is "<<biggest<<endl;
cout<<"The smallest value of the array is "<<smallest<<endl;

	return 0;
}


You can change the array to however many rand numbers you want, given you also change the for loops. This is the basics of this.
Last edited on
Topic archived. No new replies allowed.