is it possible to write this shortly?

#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
using namespace std;
int biggest(vector<int>);
int index(vector<int>);
const int k=20;
int main()
{
ofstream fout("answer.out");
vector <int> v(k);
for(int i=0; i<k; i++)
v[i]=rand() % 71+30;
fout<<"biggest is "<<biggest(v)
<<". index= "<<index(v);
}
int index(vector<int> x)
{
int big=0;
int index;
for(int i=0; i<x.size(); i++)
if(x[i]%5==0 && x[i]>big){
index=i;}
return index;
}
int biggest(vector<int> y)
{
int big=0;
int index;
for(int i=0; i<y.size(); i++)
if(y[i]%5==0 && y[i]>big){
big=y[i];}
return big;
}

is it possible to write this shortly?
Not too sure what the code is trying to accomplish, since it has no comments, no indentation, and no explanation to what it is suppose to do. but I'm guessing its trying to find the largest multiple of 5 in a randomly generated Vector of 20 elements and return it as well as its index.

To simplify that, you could completely remove the function biggest() and change the name of the function index to biggest. Then using its return value, in the main function look up the largest value using the index with a regular v[index] call or v.at(index).
Last edited on
oh sorry for the explanations, you guessed right. it works. thank you very much!
What would index() return if none of the integers in the vector<> is a multiple of five?
Garbage values left in RAM by what he wrote. If anything he should write a control statement that checks if the index is valid. The best way to do that is to set index within the function to -1. If it never gets overwritten by a valid array index it will be returned to main().

Then write a control statement to handle the -1 return such as displaying an error message.
can you write the code of what you said please? i could not understand clearly what you mean in >>setting index within the function to -1<< or here >>Then write a control statement to handle the -1 return such as displaying an error message. <<... i have an examine after two days and i don't want to fail = ((
It's not that hard.

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
31
32
33
34
35
36
37
38
#include <iostream>
#include <fstream>
#include <vector>

// Prototypes
int biggest(const std::vector<int> &);
//int index(const std::vector<int> &);

// Constant Maximum Values
const int K = 20;

int main()
{
	std::ofstream fout("answer.out");
	std::vector<int> vc(K); // Vector 'vc' with K elements of type integer

	for (int i = 0; i < K; ++i)
		v.push_back(rand() % 71 + 30);
	
	biggestIndex = biggest(vc);
	if (biggestIndex == -1)
		fout << "There was no multiple of 5 within the array.\n";
	else
		fout << "Largest integer is" << vc.at(biggestIndex) << "at index = " << biggestIndex;
}

int biggest(const std::vector<int> &vc)
{
	int max = -32768, index = -1; // index set to -1
	for (int i = 0; i < vc.size(); ++i)
		if (( (vc[i] % 5) == 0) && (vc[i] > max) ) // What if this never executes?
		{
			index = i;
			max = vc[i];
		}

	return index; // Returns index or -1 if none found
}


In function biggest I initialize index to -1.
If the loop never finds a multiple of 5, index will never change and will remain -1.
Then back in main, I simply handle this case with a control statement, outputting an error to the file.
Simple.
Great! thanks a lot.
Topic archived. No new replies allowed.