Status
Not open for further replies.

redhat

Mad and Furious
I am new to programming in C++, just now I tried to create an array with 'n' index'es where n is an input no. This is what i coded:
Code:
#include<iostream>
using namespace std;

int main()
{
    int n=10,m;
    cin >> n >> m;
    int stor[n];
    return(0);

}

But, VC++ 6.0, returns, the following errors:
Compiling...
Cpp2.cpp
C:\Documents and Settings\Darshit Shah\C++\Euler\IOI\Cpp2.cpp(8) : error C2057: expected constant expression
C:\Documents and Settings\Darshit Shah\C++\Euler\IOI\Cpp2.cpp(8) : error C2466: cannot allocate an array of constant size 0
C:\Documents and Settings\Darshit Shah\C++\Euler\IOI\Cpp2.cpp(8) : error C2133: 'stor' : unknown size
Error executing cl.exe.

Please help... how do i create such an array??
This syntax used to work in Java
 

Sykora

I see right through you.
The size of an array must be known at compile-time. ie, if you had done :

Code:
int stor[10];

it would have worked, but since the size of an array is a variable, it won't work. That's why it says "expected constant expression".

To do what you intend, try :

Code:
int* stor = new int[n];

Java != C++.
 
OP
redhat

redhat

Mad and Furious
Thanks a lot... Im new to C++, and all this hasnt been taught to us yet in college, just tried doing this with my existing programming knowledge, many syntax's worked though wasnt aware of this rule in C++

Code:
#include<iostream>
using namespace std;

int main()
{
    int n=10,m;
    cin >> n >> m;
    int* stor = new int[n];
    int* sums = new int[m];
    for(int i=0; i<n; i++)
    {
        int x;
        cin >> x;
        stor[i] = x;
    }
    for(int k=0; k<m; k++)
    {
        int s,e;
        cin >> s>> e;
        int sum=0;
        for(int j=(s-1); j<e; j++)
        {
            sum+=stor[j];
        }
        sums[m] = sum;
    }
    for(int l=0; l<m; l++)
    {
        cout << sums[l];
    }
    return(0);

}
Can someone please correct this code and tell me why am I not getting a output, after some checks I realised that the array sums[] does contain the perfect values that it should, but when trying to print it in the last loop, it is giving garbage values
 
Last edited:

Sykora

I see right through you.
What do you mean, garbage values? If it looks like a really big, but single number, then it would be because you're not inserting spaces between the numbers. Try :

Code:
cout << sums[l] << endl;

If that still doesn't work, simplify the program until you get an answer, and then add to it.
 
use the new operator for the stor.

Try int *stor new int[n];
I am sure it will work.
You need to use pointers for dynamic initialisation of an array.
 
OP
redhat

redhat

Mad and Furious
@MetalheadGautham : I HAVE used pointers for the initialisation.. see the code.. yea.. initially i wasnt aware. but now I know that I must use pointers for dynamic initialisation.

@Sykora : No, it isnt one big number.. I would've realised that folly very easily!! It shows the same particular number again and again... and that value is in no way related to the program
 

QwertyManiac

Commander in Chief
stor[] is bounded by n. Your summing loop doesn't take care of that and thus out of bound garbage values may get added!

That, and your sums[m] = sum; line must read sums[k] since that is the counter and m is the size, which is just outside the boundary.
 
Last edited:
Status
Not open for further replies.
Top Bottom