Largest Two

I am missing something on my code, I need help with this, but not the answer

#include <iostream>

using namespace std;

//Do not modify anything on or above the line below this
//START_PROVIDED


void printLargestTwo(int values[], int size)
{
int largest = values[0];//setup largest and second largest
int second = values[1]
for (int a = 0; a < size; a++)
{
if (values[a] >= largest)
{
values[a] > largest && values[b] > second; //assignment statement= over writing the box

}
else if (values[b] >= second)
{
second = values[b];
}
}
cout << largest << second;
}

//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
int NUM_VALUES; //cheating to make this flexible
cin >> NUM_VALUES;
int values[NUM_VALUES];
//Read in values
for(int i = 0; i < NUM_VALUES; i++) {
cin >> values[i];
}

printLargestTwo(values, NUM_VALUES);
}
Please use code tags for readability. Put the tags [code] and [/code] between your code segments.


The very first problem I see is that you are trying to make an array with a variable number.
Static array sizes MUST be known at COMPILE time, not run time. The size of an array cannot be set with a variable -- it must be a physical number > 0 or a const "variable".
ex:
1
2
3
const int Size = 3;
int my_arr[Size]; // valid
my_arr[2] = 42;

Edit: I see that you as the student aren't supposed to edit the stuff in main(), so don't worry about it if you aren't allowed to fix it. Your instructor should read up on arrays though ;) Edit 2: Oh I see, he knows he's "cheating".

Second,
this line
values[a] > largest && values[b] > second; //assignment statement= over writing the box
Doesn't do anything. As the comment implies (I think?) you need to use the assignment operator '=' to set your "largest" variable.
Last edited on
thank you, I revert it back to my original program. Where else should I make adjustments?
I don't know what your original program is.

You also never declare or initialize the variable "b".

I would change the line values[a] > largest && values[b] > second;
to simply
largest = values[a];
And change every place you have a "b" to an "a".


Also, be careful of your initial values
1
2
int largest = values[0];//setup largest and second largest
int second = values[1]

If the array is {3, 6}, the logic below this will get messed up.
I would initialize both values to -1 or something.
Last edited on
here is the assignment
The existing code is trying to call a function printLargestTwo that should take in an array of ints and its size and prints the two largest values from the array. Write that function.
Hint: Start by finding just the largest item (you've seen an example of find smallest...). Then modify it to also look for second largest... Each new number you see is either A: the largest (in which case the old largest is now the second largest), B: the new second largest, or C: neither the new largest nor second largest.

#include <iostream>

using namespace std;

//Do not modify anything on or above the line below this
//START_PROVIDED


void printLargestTwo(int values[], int size)
{
int largest = values[a];//setup largest and second largest
int second = values[a]
for (int a = 0; a < size; a++)
{
if (values[a] >= largest)
{

largest = values[a]; //assignment statement= over writing the box

}
else if (values[a] >= second)
{
second = values[a];
}
}
cout << largest << second;
}

//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
int NUM_VALUES; //cheating to make this flexible
cin >> NUM_VALUES;
int values[NUM_VALUES];
//Read in values
for(int i = 0; i < NUM_VALUES; i++) {
cin >> values[i];
}

printLargestTwo(values, NUM_VALUES);
}
You're still not using code tags, read my first post.

So I'm not clear what's the problem you're having?
On the first lines if your function, you have
1
2
int largest = values[a];//setup largest and second largest
int second = values[a]

You're missing a semi-colon, and the variable "a" is not declared. I would just replace the a with 0.
Last edited on
#include <iostream>

using namespace std;

//Do not modify anything on or above the line below this
//START_PROVIDED


void printLargestTwo(int values[], int size)
{
int largest = values[0];//setup largest and second largest
int second = values[0];
for (int a = 0; a < size; a++)
{
if (values[a] >= largest)
{
largest = values[a]; //assignment statement= over writing the box
}
else if (values[a] >= second)
{
second = values[a];

}
}
cout << largest << " " << second << " " << endl;
}

//END_PROVIDED
//Do not modify anything on or below the line above this

int main()
{
int NUM_VALUES; //cheating to make this flexible
cin >> NUM_VALUES;
int values[NUM_VALUES];
//Read in values
for(int i = 0; i < NUM_VALUES; i++) {
cin >> values[i];
}

printLargestTwo(values, NUM_VALUES);
}


here is the results part
Results:

Results:

Status Input Output Expected
Fail
4
10 12 14 8
14 10
14 12
Pass
5
1 2 5 4 3
5 4
5 4
Fail
3
10 8 6
10 10
10 8
Pass
3
8 10 6
10 8
10 8

closed account (D80DSL3A)
Your algorithm is missing ONE line of code.
1
2
3
4
5
6
7
8
9
10
if (values[a] >= largest)
{
// a line of code belongs here
largest = values[a]; //assignment statement= over writing the box
}
else if (values[a] >= second)
{
second = values[a];

}

Hint from the problem description:
Each new number you see is either A: the largest (in which case the old largest is now the second largest)

So, you need to make the second largest = the old largest.
Last edited on
Topic archived. No new replies allowed.