+ 1

Where am I getting this wrong???. The code provided calculates the largest element of the array nums and outputs it.

https://code.sololearn.com/cs5yRddagx1m/?ref=app

16th Oct 2022, 9:11 AM
Abraham Zimba
Abraham Zimba - avatar
4 ответов
+ 1
Thanks to Jayakrishna🇮🇳 for the pointers. Nailed it!
16th Oct 2022, 12:54 PM
Abraham Zimba
Abraham Zimba - avatar
+ 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..
16th Oct 2022, 1:01 PM
Jayakrishna 🇮🇳
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... ?
16th Oct 2022, 10:54 AM
Jayakrishna 🇮🇳
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; }
16th Oct 2022, 12:53 PM
Abraham Zimba
Abraham Zimba - avatar