+ 2
Dynamic array allocation to find max number in array?
Here is my code , that is not working please anyone help? https://code.sololearn.com/c96NGkLk7a9C/?ref=app
4 Réponses
0
You did not declare nums, try this it will take the length and the array from the user
#include <iostream>
using namespace std;
int main() {
int n;
cin>>n; //size of the array
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;
return 0;
}
+ 1
Ruba Kh
Yes it works but i want to try like
int *ptr = new int;
Something like this if you have any idea.
+ 1
maybe like that?
#include <iostream>
using namespace std;
int main() {
int n;
int * nums;
cin>>n; //size of the array
// your code goes here
nums = new int[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;
return 0;
}