Question:

C programming: simple array question

by  |  earlier

0 LIKES UnLike

say I have an array of size i and the user decides the size of the array and the integers that go into the array.

so for example the user chooses an array size of 6, and types in 1,2,3,4,5,6 as the integers, how can I then print these values that the user typed in which were stored into the array?

 Tags:

   Report

3 ANSWERS


  1. for(int j = 0; size < i; j++);

    {

    cout << array[j] << endl;

    }

    That will print out the array to the console which each integer going to one line. The basic run through is this:

    1.) j is zero, so cout array[0] (0 is the first position in an array)

    2.) j is increased by 1, making it 1, so cout array[1]

    3.) process repeats until j is eqaul to i. This is because it will keep increasing and going through the loop until when j is equal to i and the loop does not execute.

    Look at my source link for an article on for loops. I have also provided a c++ cheat sheet.


  2. You need to use "new" to allocate such an array and "delete" to free the memory when you are done using it. That is called dynamic allocation:

    http://xoax.net/comp/cpp/reference/refsa...

    See example 5 under the pointer section.


  3. you cannot have an array without a constant size (eg, a variable wont work)  it must be a number or a constant.

    This is the case unless you have a dynamic array.  sorry, but normal arrays won't work.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.