C++ Array help ASAP!

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.
It's fine. Ideally use std::size_t for array indexing and loop counting, and avoid using the magic number 4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int array[] = {20, 23, 24, 48, 532} ;
    // http://en.cppreference.com/w/cpp/types/size_t
    const std::size_t SZ = sizeof(array) / sizeof(int) ;

    // position in the array to store the value entered by the user
    const std::size_t USER_POS = 1 ;

    std::cout << "Please enter an Integer value: ";
    std::cin >> array[USER_POS] ;

    for( std::size_t i = 0 ; i < SZ ; ++i )
    {
        std::cout << "array[" << i << "] == " << array[i] ;

        if( i == USER_POS ) std::cout << " (user entered value)\n" ;
        else std::cout << '\n' ;
    }
}
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.
See: https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants

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.
what other resourses can you recommend for beginners?
A friend of mine recommended this free one:
https://www.edx.org/course/introduction-c-microsoft-dev210x-6
You don't have to pay for the certificate.

Or maybe this one: https://isocpp.org/blog/2013/11/learn-how-to-program...-with-c-kate-gregory
Here are some useful resources for beginners, off the cuff.

C++ Idioms - answers to 'How do (should) I do this in C++': https://cpppatterns.com/

General C++ programming guidelines / best practices:
https://docs.microsoft.com/en-us/cpp/cpp/welcome-back-to-cpp-modern-cpp

Text Book (University of Groningen): http://www.icce.rug.nl/documents/cplusplus/

FAQ: https://isocpp.org/faq


A few, which are also very useful, though they are not specially targeted towards beginners.

Language Library reference: http://en.cppreference.com/w/cpp

Core Guidelines:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md

More C++ Idiioms: https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms
A friend of mine recommended this free one:
https://www.edx.org/course/introduction-c-microsoft-dev210x-6
You don't have to pay for the certificate.


I completed the course but grading was having problems. I would still recommend the course. I am currently taking the sequel which is Intermediate C++
Topic archived. No new replies allowed.