display number in array

hye guys,
how to repair this coding?

-request size array from user,
-request numbers (any) from user,
-display numbers that has been input

and, that is my coding so far...when i compile it, windows says "cannot run"
-_-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()
{
        int arraysize;
        int a=arraysize;
        float num;
        float number[arraysize];
       
        cout<<"Array size : ";
        cin>>a;
 
        while (num<arraysize)
                {
                        cout<<"input number : ";
                        cin>>num;
                }
 
        cout<<"Number in array : ";
        cout<<num;
       
        system ("PAUSE");
        return 0;
}
Last edited on
You haven't initialized arraysize and aren't legally allowed to create an array from a non const variable. However, there is a way to create a dynamic array using pointers.

A lot of other people have been having issues with this problem so I'm going to write up a small how-to for you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   // Skip some stuff
   int numberOfElements = 0;
   int *myArray; // Going to hold our array

   std::cout << "Please enter the amount of numbers you want to enter (larger than 0): ";
   std::cin >> numberOfElements;
   // Should do some error checking to make sure the number is positive

   // We need to allocate space for our array
   myArray = new int[numberOfElements];

   // We need to add the numbers into our array
   for (int i = 0; i < numberOfElements; i ++) {
      std::cout << "Please enter number " << i + 1 << ": ";
      std::cin >> myArray[i];
   }

   // Display values
   for (int i = 0; i < numberOfElements; i ++)
      std::cout << "\nNumber " << i + 1 << ": " << myArray[i];

   delete[] myArray;
   // Skip some other stuff 


Output:
Please enter the amount of numbers you want to enter (larger than 0): 5
Please enter number 1: 1
Please enter number 2: 2
Please enter number 3: 3
Please enter number 4: 4
Please enter number 5: 5

Number 1: 1
Number 2: 2
Number 3: 3
Number 4: 4
Number 5: 5


Editted to prevent memory leak.
Last edited on
what is meant by pointer?
why must use * in int *myArray; ..?

the std::cin >> myArray[i]; i mean i is used to increase counting or to hold value? :(
An array is nothing more than a pointer. To understand an array, you must first understand memory and pointers.

All variables in stored somewhere in memory. A pointer stores that memory location, while a basic object, an int variable, etc., will store the data located at that memory location.

An array is a set of contiguous memory blocks of a specified size.When creating an array, the system must set aside x amount of blocks times the size of the object. Each object is defined to be a certain size depending on what the requirements of said object are. A char, for example, takes up one byte, an int takes up 4, etc. (Note: These are typical values but not always guaranteed). So, if we're creating an array of 10 char, we need 10bytes allocated for us. These need to be in order for the array to work properly.

When an array is actually created, it doesn't store all of the information inside of one variable, instead, it just points to the first element. This is helpful in the fact that all of the elements are separated by the size of the object. Without using any C terminology here (which goes further into depth of how everything works), this is where the [] operator comes in handy. When the [] operator is used on a pointer, the pointer is effectively increased by the number inside of the brackets, and dereferenced. This allows us to modify the value stored at that memory location, or display it to the screen, or do whatever we want with it.

I know I've probably lost you by this point, but try to bare with me. I've typed up a short example demonstrating how an array actually works.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main() {
   int myArray[10];
   
   for (int i = 0; i < 10; i ++)
      myArray[i] = i;
   
   for (int i = 0; i < 10; i ++)
      std::cout << myArray[i] << " is located at " << &myArray[i] << "\n";
   return 0;
}


On my system, this is the output:
0 is located at 0x22ff08
1 is located at 0x22ff0c
2 is located at 0x22ff10
3 is located at 0x22ff14
4 is located at 0x22ff18
5 is located at 0x22ff1c
6 is located at 0x22ff20
7 is located at 0x22ff24
8 is located at 0x22ff28
9 is located at 0x22ff2c


If you notice, each memory location is exactly 4 bytes apart, the exact size of an int.

Now, to where this all plays into what I previously posted. As I said, an array needs to be contiguous. In order to make an array larger, or smaller if so desired, we need to allocate memory. This is where the new comes into play. Your user will define the size of the array. However, in C++, the standard requires that arrays are generated using a const value, so that it knows exactly how much to allocate for the array. This is why we need to "new" memory for our array. new does some fancy stuff behind the scenes, but it's essentially the same thing as declaring a new array, the only exception is that it returns the memory location of the newly allocated memory for us to use.

I rewrote the example I gave you earlier:
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
26
#include <iostream>

int main() {
   int numberOfElements = 0;
   int *myArray; // Going to hold our array

   std::cout << "Please enter the amount of numbers you want to enter (larger than 0): ";
   std::cin >> numberOfElements;
   // Should do some error checking to make sure the number is positive

   // We need to allocate space for our array
   myArray = new int[numberOfElements];

   // We need to add the numbers into our array
   for (int i = 0; i < numberOfElements; i ++) {
      std::cout << "Please enter number " << i + 1 << ": ";
      std::cin >> myArray[i];
   }

   // Display values
   for (int i = 0; i < numberOfElements; i ++)
      std::cout << "\nNumber " << i + 1 << ": " << myArray[i] << " @ " << &myArray[i];

   delete[] myArray;
   return 0;
}


Notice the output on my system:
Please enter the amount of numbers you want to enter (larger than 0): 5
Please enter number 1: 1
Please enter number 2: 2
Please enter number 3: 3
Please enter number 4: 4
Please enter number 5: 5

Number 1: 1 @ 0x3e2550
Number 2: 2 @ 0x3e2554
Number 3: 3 @ 0x3e2558
Number 4: 4 @ 0x3e255c
Number 5: 5 @ 0x3e2560


Again, each int is exactly 4 bytes apart. This is necessary so that the values are all in order, with exactly the same width away from each other.

Don't be afraid if this is over your head, it took me years to not be afraid of pointers and things that involve memory, however, it's worthwhile to understand why things need to be the way they are.

So you might ask, what happens if you don't allocate enough memory? Well, this really depends. Sometimes your program might attempt to access memory that's in use by another program. This will cause your program to crash. Sometimes, your program might attempt to access memory which hasn't been allocated to it yet, this can result in a crash, or not, it really depends on a lot of factors.

I'm sort of just rambling on now, so if you have any more questions, please ask.
Topic archived. No new replies allowed.