hey can anyone tell me about the compiler turbo c++ , i want to use the variable array like a[i] if we want to enter a constant of our own choice ..... plz help this is what i did , correct me if i m wrong !
int i;
float a[];
but this isnt working :( it shows an error enter a constant array value
You should put constant numerical value inside of the '[]' characters of an array declaration in order to tell the compiler how large you want it to be.
You could also pick virtually any one of the containers from the STL, those are nice for beginners because they take care of all of the gritty stuff: http://www.cplusplus.com/reference/stl/
You do need to learn how to manage memory at some point though. They're a little intimidating but they really aren't that difficult to use once "it clicks".
If you are talking about the really old Turbo C++ compiler from Borland, it is pre-standard C++.
If you are talking about the short time that Embarcadero was offering Turbo C++ Express (I think that was what it was called), then you are much better off.
Because if not, you might want to get a more modern compiler.
1 2 3 4 5 6 7
unsigned n;
...
float* a = newfloat[ n ];
// mess with a[] here.
delete [] a;
Be aware that if exceptions can occur in the code between new and delete, you need to trap them and delete before passing them along:
1 2 3 4 5 6 7 8 9 10 11 12
unsigned n;
...
float* a = newfloat[ n ];
try {
// mess with a[] here
}
catch (...) {
delete [] a;
throw;
}
delete [] a;
This is what i did by passing the variable to the functions by using array
#include<iostream.h>
#include<conio.h>
//float abcd[];
void safia(float abc[],int a);
int main()
{
clrscr();
int a; float abc[]={1};
cout<<"enter ";
cin>>a;
safia(abc,a);
return 0;
}
void safia(float abc[],int a)
{ for(int b=0;b<a; b++)
{ cin>>abc[a];
cout<<abc[a]<<endl;} }
but i m unable to understand why i have to use this float abc[]={1};
The program has to know how much memory to set aside for your array, it is smart enough to discern the intended size of the array if you are assigning the values on declaration like you do here.
It is incorrect. In order to explain, I need to change one thing:
float abc[] = {7};
says is that you are creating an array (named 'abc') that has the number of elements given by the initializer list. The initializer list contains exactly one element (the number '7'), so the array is an array of one element initialized to the value '7'.
The safia() function uses however many elements the user inputs, which is probably more than one. This will cause a crash or corrupted data.
#include <iostream.h>
void safia( float abc[], unsigned size_of_abc )
{
// Do something (random) with the array.
for (unsigned n = 0; n < size_of_abc; n++)
abc[ n ] = n * 2;
// Print what we did to the array
for (unsigned n = 0; n < size_of_abc; n++)
cout << "abc[ " << n << " ] = " << abc[ n ] << "\n";
}
int main()
{
unsigned n;
cout << "How many elements? ";
cin >> n;
float* abc = newfloat[ n ]; // Create the array with 'n' elements
safia( abc, n ); // Use the n-element array
delete [] abc; // Destroy the array
return 0;
}
#include<iostream.h>
#include<conio.h>
void safia(float abc[], int a);
int main()
{
clrscr();
int a;
float abc[]={1};
cout<<"Enter Number of Inputs ";
cin>>a;
safia(abc,a);
return 0;
}
void safia(float abc[], int a)
{ float sum1=0, sum2=0, sum3=0;
for(int b=0; b<a; b++)
{ cin>>abc[a];
if(abc[a]>=0)
sum2=sum2+abc[a];
if(abc[a]<=0)
sum3=sum3+abc[a];
sum1=sum1+abc[a]; }
cout<<"Sum of all Numbers "<<sum1<<endl;
cout<<"Average of all Numbers "<<sum1/a<<endl;
cout<<"sum of Positive Numbers "<<sum2<<endl;
cout<<"Average of Positive Numbers "<<sum2/a<<endl;
cout<<"Sum of Non-Positive Numbers "<<sum3<<endl;
cout<<"Average of Non-Positive Numbers "<<sum3/a<<endl;
}
but the problem that is arising is that in output screen , along with the required result at the end it shows *null pointer assigment* ! What does it mean ??
float abc[]={1};
cout<<"Enter Number of Inputs ";
cin>>a;
safia(abc,a);
This is wrong.
Your 'abc' array has a fixed size of 1. If the user inputs a number larger than 1, you will step outside the bounds of your array and will corrupt memory.
The new line allocates n floats on the heap and stores the address of the first float in the pointer variable 'abc'.
When you are done with the memory (the array of floats allocated on the heap) you must free it using delete.
Please be aware that Disch may not have seen our posts before he put in his reply. (It happens all the time. You gotta get up and go to the bathroom, or change your kid's diaper, or the internet is responding slowly, or your brain is responding slowly, etc.)