Hi there, I am new to C++ and need more eyes to see if this is correct. This is the instructions:
Define an array data structure with at least 5 items, which will be used to store integers.
Initialize the array with any different integer values.
Prompt the user to enter an Integer value.
Choose a position in the array which is different from the position in the sample code, and store the value that the user just entered.
Construct a logic structure to iterate through the array and print out the content of each position in the array.
When the index where the user entered value is stored is encountered, use a logic structure to print a unique print statement indicating the index of where the user entered value is stored.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int myArray [5] = {20, 23, 24, 48, 532};
int arraySize = sizeof(myArray) / sizeof(int);
cout << "Please enter an Integer value: ";
cin >> myArray[4]; // Choose a different position here
for(int k=0; k < arraySize; k++)
{
if(k == 4)
{
cout << "This array position " << k << " has stored the user entered value as: " << myArray[k] << "\n";
}
else
{
cout << "The array position " << k << " contains value " << myArray[k] << "\n";
}
}
return 0;
}
I just want to confirm I did this assignment properly please assist and thank you for any help provided.
Hi JLBorges I apologize for my ignorance I am just getting into this. Can you explain to me why not to use “magic number 4” just briefly if you can tell me why? Again I apologize for my ignorance.
In this program, this would be the compelling reason not to use a magic number:
It is easier to alter the value of the number, as it is not duplicated. Changing the value of a magic number is error-prone, because the same value is often used several times in different places within a program.