0
please solution in the practice question of c++ of dynamic memory. Sample input are 4,12,7,9 and 34. Sample output is 34.
C++ #include <iostream> using namespace std; int main() { int n; cin>>n; //size of the array //your code goes here int max = nums[0]; for(int i=0; i<n; i++) { if(nums[i]>max) max = nums[i]; } cout << max; return 0; }
5 ответов
+ 2
/*in Sololearn input box, type:
4 12 7 9 34
Submit
*/
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n; //size of the array
//your code goes here
int nums[n];
for(int i=0; i<n; i++) {
cin >> nums[i];
}
int max = nums[0];
for(int i=0; i<n; i++) {
if(nums[i]>max)
max = nums[i];
}
cout << max;
}
+ 1
Create an array of size n
int* nums = new int[n];
Then loop n times to assign input values to the array
for (int j = 0; j < n; j++) {
cin>>nums[j];
}
+ 1
chukwudi anthony mbegbu
Probably not relevant, since the lesson was possibly about how to create arrays, but you don't even have to create an array if you only want max.
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
int max = 0;
for(int i=0; i<n; i++) {
cin>>n;
if(n>max)
max = n;
}
cout << max;
}
+ 1
Bob_Li thanks
0
Please can you type how i should code. How can one initialize and declare the nums?