Question:

C programming: arrays 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

4 ANSWERS


  1. I don't believe the answer jrom posted will work, if you're using gcc as the compiler, as gcc requires you to declare the size of the array at the beginning of the function.  But, there is a work around...make a new function with the input size as an argument, and declare the size of the array inside that function.

    Answer:

    http://img107.imagevenue.com/img.php?ima...

    Of course...this is simply a way to avoid having to use malloc.  It may not be considered good programming practice, but for little tasks like this...why not?


  2. What part of the question are you having problems with?

    1) Ask user for the array size.

    2) Malloc that much memory.

    3) Put user inputs in array in a loop.

    4) Print each item in a loop.

    Which step is the problem?

  3. I haven't written in C for a while but I beleive you can just do:

    for(int count; count < i; count++)

    {

         print myArray[count] + "\n";

    }

  4. 1.first thing you do is PROMPT the user asking for the ARRAY SIZE:

    example: Please input array size:

    2, then get his input and put it in a variable say,

    int arraySize;

    3. then declare your array with the specific array size based on his input.

    sample:

    int[] arrNumbers = new int[arraySize];

    4. then loop through each array PROMPTING the user to input an array and store it on the array index

    sample:

    for(int i = 0; i < arraySize ; i++)

    {

       println("input number: ")

       // then read his input and store it in your array

       arrNumbers[i] = userinput;

    }

    5. to display, just loop thru each array values and print.

    hope this helps

Question Stats

Latest activity: earlier.
This question has 4 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions