I'm trying to find the second largest number in my array of 10 and im getting really wierd errors of
class.cpp: In function âint secondLargest(int*, int)â:
class.cpp:11: error: name lookup of âiâ changed for new ISO âforâ scoping
class.cpp:7: error: using obsolete binding at âiâ
this is my code.
#include <iostream>
using namespace std;
int secondLargest(int a[], int size) {
int currentMax = a[0];
int secondMax=0;
for( int i = 0; i< size; i++) {
if (a[i] >secondMax)
secondMax = a[i]; }
if (a[i]>currentMax){
secondMax=currentMax;
currentMax=a[i];
}
return secondMax;
}
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout << "The max of the array is :" << secondLargest(a,10) << endl;
for( int i = 0; i< size; i++) {
if (a[i] >secondMax)
secondMax = a[i]; } // Closes for loop
if (a[i]>currentMax){
secondMax=currentMax;
currentMax=a[i];
}
You close your for loop on accident after the first if statement. Change it to this and it works
1 2 3 4 5 6 7 8 9
for( int i = 0; i< size; i++) {
if (a[i] > secondMax)
secondMax = a[i];
if (a[i] > currentMax){
secondMax=currentMax;
currentMax=a[i];
}
}
#include <iostream>
usingnamespace std;
int secondLargest(int a[], int size) {
int currentMax = a[0];
int secondMax=0;
for( int i = 0; i< size; i++) {
if (a[i] >secondMax)
secondMax = a[i];
if (a[i]>currentMax){
secondMax=currentMax;
currentMax=a[i];
}
return secondMax;
}
}
int main () {
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout << "The max of the array is :" << secondLargest(a,10) << endl;
return 0;
}