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];
}
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:
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.
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];
}
//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];
}
if (values[a] >= largest)
{
// a line of code belongs here
largest = values[a]; //assignment statement= over writing the box
}
elseif (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.