I am new to C and I am trying to store these numbers : 78, 29, 30, 27, 30 into an integer array according to its index.
This is my current code:
#include <iostream>
using namespace std;
int main() {
int numarray [5] = {78, 29, 30, 27, 30};
for (int i = 0; i < 5; i) {
numarray[i]
}
}
CodePudding user response:
int numarray [5] means your array has only 5 indices i.e 0, 1, 2, 3, 4.
You need to have an array big enough. The biggest number in your array is 78 so you need to have an array of size 79.
int numarray[79]; numarray[78] = 78
This will work.
CodePudding user response:
If I understand you correctly, you want to store a number in a container with a custom (user-defined) index.
C-style array (which numarray
is) is a sequence container. It contains a sequence of values, an index is used only to get the value from it.
What you are looking for is an associative container (a key-value map).
Take a look at std::map
.