Answer to Question #160983 in C++ for Ashes

Question #160983

Write a C++ program to accept five integer values in an array using pointer offset notation.


1
Expert's answer
2021-02-03T11:18:23-0500
#include <iostream>

int main()
{
    const int ARRAY_SIZE = 5;

    int data[ARRAY_SIZE];

    std::cout << "Please enter " << ARRAY_SIZE << " integers: ";
    
    for(int i=0; i<ARRAY_SIZE; ++i)
    {
        std::cin >> *(data + i);

            if(!std::cin)
            {
                std::cout << "Bad input\n";
                return 1;
            }
    }

    std::cout << "Array data: ";
    
    for(int i=0; i<ARRAY_SIZE; ++i)
    {
        std::cout << *(data + i) << (i < ARRAY_SIZE - 1 ? " " : "\n");
    }    
    
    return 0;    
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog