C++ Help for Array

msapumal

Member
Feb 16, 2012
18
1
0
Can anybody please tell me, How to declare a array for following variable using C++

#Dont know size of the array
#Dont know the values which input i to arrays

//Marks is the array name
//count is array size
// x is input value by user
Example- int Marks[count]={x}

I want a simple solution
 

danu91

Well-known member
  • May 9, 2009
    1,108
    64
    48
    int count = 1000000;
    int arr [count];
    cin >> count;
    for (int x=0; x <= count ; x++){
    arr;
    }
     
    Last edited:

    danu91

    Well-known member
  • May 9, 2009
    1,108
    64
    48
    or you can use a vector.. but i dont know whether C++ have vectors... in java and c# you can easily use a vector for ur prob!

    www.cplusplus.com
    refer this website!!
     
    Last edited:

    msapumal

    Member
    Feb 16, 2012
    18
    1
    0
    count = 1000000

    is this type of variable declaration is a good programming behavioral?
    I mean, is this valid for professional programming?
     

    danu91

    Well-known member
  • May 9, 2009
    1,108
    64
    48
    he's asking for a simple solution right??
    soo, i guess da simplest way is dat, although ur solution is theoretically better than mine :)
     

    msapumal

    Member
    Feb 16, 2012
    18
    1
    0
    Thank you pal,
    Can you please simply explain me about dynamic memory allocation.
    I'm new to C++ programming and I have a course work to do...
    :(
     

    danu91

    Well-known member
  • May 9, 2009
    1,108
    64
    48
    count = 1000000

    is this type of variable declaration is a good programming behavioral?
    I mean, is this valid for professional programming?

    professional method is to use pointers. but it depends on how much u have learned and how much u are willing to learn!!
    btw, are u from iit?
     

    msapumal

    Member
    Feb 16, 2012
    18
    1
    0
    he he....:lol:
    then is it ok 2 use that sort of declaration for this CW?
    I mean, is it not a problm for Mr. Dulip? :)

    btw nice meet u ....:lol:
     

    danu91

    Well-known member
  • May 9, 2009
    1,108
    64
    48
    he he....:lol:
    then is it ok 2 use that sort of declaration for this CW?
    I mean, is it not a problm for Mr. Dulip? :)

    btw nice meet u ....:lol:

    nice 2 meet u 2 machan....
    anyway, dulip didnt teach us.. it was Ishara akka hu taught us!.. but, if dulip hasn't taught u pointers, then u are allowed 2 create a big enough counter variable and use it.. if u want, juz send him a email and see.. anyway, we used bigger enough counter variables in our 1st year cw!
     

    msapumal

    Member
    Feb 16, 2012
    18
    1
    0
    nice 2 meet u 2 machan....
    anyway, dulip didnt teach us.. it was Ishara akka hu taught us!.. but, if dulip hasn't taught u pointers, then u are allowed 2 create a big enough counter variable and use it.. if u want, juz send him a email and see.. anyway, we used bigger enough counter variables in our 1st year cw!

    Thanx a lot..:cool:
     

    ¤--bACarDi--¤

    Well-known member
  • Jan 9, 2009
    12,130
    288
    83
    124.43.xxx.xxx
    dynamic memory allocation means that you are taking memory from Heap
    new in c++ or malloc in C will give you memory from the heap.

    so when you don't know the size you'll need, go with it
     

    rokie

    Member
    yeah Dynamic Array initializing is the most safest way to your question

    but your second condition has a problem
    it is that
    you've told that u don't know which values you've going to input

    in concept of initializing arrays you must know which types of values that u going to enter to your array that means the type (int ,String ,unint, binary etc)-- i am Not a professional in c++ but I've qualified in J2SE platform very much.

    I don't think that there are much more variations in java and c++ either.......
    But initializing variables which u don't know where it is situated in the nameSpace won't be a good thing in programing ....
    don't worry u'll learn eventually.......... the types of variables

    in your case initializing the 'count' variable is must it is a good behavior that you've assigned a very big number to it............

    imagine that you've entered a length of 1000001 values into the array
    what else can u do ??????????????????
     

    Dead Island

    Member
    Mar 22, 2012
    5,350
    682
    0
    what's the point of C++ if u don't use STL classes?
    so use Vector or List. if u use C stick to Arrays.
    if the language has inbuilt features use them that's why they are for.
     
    Jan 19, 2013
    14
    32
    0
    bandarawela
    simply u can use vector with STL.but it is not a part of pure c++ it is a pre written code for that can use for sizable arrays.also u can do with pure c++ with dianamic memory allocations and pointers.the simple idea is if the size is not enough create a array that have previous array's size+1 size and copy all the array in to the newly created array.and ready to get new input.the code get some loops and functions.OR simply u can write...

    #include <iostream>
    usning namespace std;
    int main ()
    {
    int count = 0;
    cout << "enter the size of array : ";
    cin >> count;
    int array[count];
    for (int i = 0; i < count; ++i)
    {
    cout << "enter a value to insert to the array : ";
    cin >> array;
    cout << "inserted to the array !"<<endl;
    }
    return 0;
    }