+ 1
Where am I getting this wrong???. The code provided calculates the largest element of the array nums and outputs it.
4 odpowiedzi
+ 1
Thanks to Jayakrishna🇮🇳 for the pointers.
Nailed it!
+ 1
Yes. That works. But to reduce it, you can loop from 0 to n.
If you want use input data further you can use an array..
Like:
#include <iostream>
using namespace std;
int main() {
int n, nums=0, max =0; //assign 0
cin>>n;
for(int i=0;i<n; i++) {
cin>>nums;
if(nums>max){
max=nums;
}
}
cout<<max<<endl;
return 0;
}
For array, take nums[i] instead of num.
initialize int nums[5];
Hope it helps..
0
cin>>n; cin>>nums;
what is n, num here? Why you need 2 inputs?
for(int i=0; i<n; i++) {
if(int i=1;i<n;i++){ // this is invalid syntax. What are you trying here... ?
0
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int nums=0;
cin>>nums;
int max = nums;
for(int i=1;i<n;i++) {
cin>>nums;
if(nums>max){
max=nums;
}
}
cout<<max<<endl;
return 0;
}