short question

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.
but if we want to take the value from the user then ????
That's called dynamic memory allocation: http://www.cplusplus.com/doc/tutorial/dynamic/
either over dimension it to a large no like 1000 (assuming u are not making any big project in turbo) or master pointers as Computergeek1 said
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 = new float[ 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 = new float[ n ];

try {
  // mess with a[] here
}
catch (...) {
  delete [] a;
  throw;
}
delete [] a;

Hope this helps.
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};
Last edited on
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.

Use the methods already shown to you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#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 = new float[ n ];  // Create the array with 'n' elements
  safia( abc, n );              // Use the n-element array
  delete [] abc;                // Destroy the array

  return 0;
  }
How many elements? 12
abc[ 0 ] = 0
abc[ 1 ] = 2
abc[ 2 ] = 4
abc[ 3 ] = 6
abc[ 4 ] = 8
abc[ 5 ] = 10
abc[ 6 ] = 12
abc[ 7 ] = 14
abc[ 8 ] = 16
abc[ 9 ] = 18
abc[ 10 ] = 20
abc[ 11 ] = 22

Notice also that using unsigned makes it impossible to have a negative number of elements.


BTW, you should get yourself a more modern compiler. The old Turbo C++ stuff is outdated.

Hope this helps.

thank you all :) here is my final programme

#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 ??
what exactly is the meaning of b/m line. are u using pointers?

float* abc = new float[ n ];
1
2
3
4
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.

Reread Duoas earlier post as he explains this.
changes made as per duoas , but please explain this

float* abc = new float[ n ];

as it removes the error of Null point assigment ! i cannot comprehend above line. is it about pointers or assigning memory to array?
Yes, it is a pointer to float.

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.)


Here is some reading for you about arrays:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.cplusplus.com/faq/sequences/#what-is-a-sequence

and pointers:
http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/faq/sequences/strings/c-strings-and-pointers/

Hope this helps.
Topic archived. No new replies allowed.