I have been trying to use variable as size of an array(e.g.float data[x]) but can't do it. I have searched for others answers and can't seem to understand those. Please explain me in simplest way on how to use variable as size of array in turbo C++.
//Program to print largest element in an array(using a function)
#include<iostream.h>
#include<conio.h>
float largest(float abc[],int n)
{
float large=0;
for(int k=0;k<n;k++)
{
if(large<abc[k])
{
large=abc[k];
}
}
return large;
}
int main()
{
clrscr();
cout<<"In this program you can enter any amount of elements you";
cout<<" want and it will give the largest element as an output.\n";
int noe;//from here to---------------------------
cout<<"Enter the amount of elements you want to enter :";
cin>>noe;
float data[5];
if(noe>5)
{
data[noe];
}// here--------------------
for(int i=0;i<noe;i++)
{
cout<<"Enter element no."<<(i+1)<<" : ";
cin>>data[i];
}
cout<<"Largest element in the array is : "<<largest(data,noe);
getch();
return 0;
}
An even better and easier solution is to use a modern compiler and use std::vector.
Or forget C++ and learn C# or Java, even the oldest versions have container classes.