Question

The following C++ code has a problem where the loop can exceed the size of the...

The following C++ code has a problem where the loop can exceed the size of the array if the user inputs too many numbers.

#include <iostream> using namespace std;

int main() {

int nums[20] = { 0 };

int a[10] = { 0 };

cout << a << endl;

cout << nums << endl;

cout << "How many numbers? (max of 10)" << endl; cin >> nums[0];
for (int i = 0; i < nums[0]; i++)
{

cout << "Enter number " << i << endl;

cin >> a[i]; }

// Output the numbers entered

for (int i = 0; i < 10; i++) cout << a[i] << endl;

return 0; }

If this program is run and we enter 255 for how many numbers, and 9 for every single number, then the program does something like this:

How many numbers? (max of 10)
255
Enter number 0
9
Enter number 1
9
Enter number 2
9
Enter number 3
9
Enter number 4
9
Enter number 5
9
Enter number 6
9
Enter number 7
9
Enter number 8
9
Enter number 9
9
Enter number 10
9
Enter number 11
9
Enter number 12
9

9
9
9
9
9
9
9
9
9
(Program may crash at this point, or possibly just exit)

Your results may vary, depending on what compiler, operating system, and CPU that you are using. If the program does not crash on your compiler, try it on transformer or the Windows machines in the lab and you should get the behavior shown above.

Why didn't the program loop 255 times? Your answer should be detailed and reference the way in which memory is organized and what is put into memory.

I don't really understand how memory works. Could somehow help me understand what is going on when this program runs in memory?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
using namespace std;

int main() {

int nums[20] = { 0 };

int a[10] = { 0 };
int n;//reading size into n

cout << a << endl;

cout << nums << endl;

//reading size into separate variable
cout << "How many numbers? (max of 10)" << endl;
cin >> n;
for (int i = 0; i < n; i++)
{

cout << "Enter number " << i << endl;

cin >> a[i];
  
}

// Output the numbers entered

for (int i = 0; i < n; i++) cout << a[i] << endl;

return 0;
  
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
The following C++ code has a problem where the loop can exceed the size of the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a new file (in Dev C++) Modify program above to use a vector instead of...

    Create a new file (in Dev C++) Modify program above to use a vector instead of an array. Header comments must be present Prototypes must be present if functions are used Hello and goodbye messages must be shown Vector(s) must be used for implementation Use comments and good style practices ====================================================================================================== #include <iostream> using namespace std; int main() { cout<<"Welcome! This program will find the minimum and maximum numbers in an array."<<endl; cout<<"You will be asked to enter a number...

  • I need help finding the several errors and several loop errors for I get the final...

    I need help finding the several errors and several loop errors for I get the final output. C++ cLion #include <iostream> using namespace std; int main() { cout << "Welcome to Loop World" << endl; cout << endl; cout << "******************" << endl; cout << "Section I" << endl; cout << "******************" << endl; int sum; // Accumulates the total int i; // Used as loop control variable int numIter; // The number of times to iterate cout << "Enter...

  • What does this error message mean? "expected primary expression before int" Also, how do i get...

    What does this error message mean? "expected primary expression before int" Also, how do i get the program to repeat the numbers that were entered by the user back to me? this is my code below: // This program accepts a series of positive numbers // This code was last modified on 2/16/2020 #include <iostream> #include <iomanip> #include <cmath> using namespace std; void welcome(); int entrySubmission(int numbers); void acceptsNumbers(int); void goodbye(); int main() { welcome(); entrySubmission(int numbers); acceptsNumbers(int); goodbye(); return...

  • 6. (Short answer) The C++ code below is very close to working, but, as written, won't...

    6. (Short answer) The C++ code below is very close to working, but, as written, won't compile. When trying to compile, the compiler generates a number of errors similar to "cout' was not declared in this scope." Respond to the prompts below about the code.(20 points, 5 points each) #include <iostream> void FindMax(int a, int b, int& max) { if (a > b) max = a; else { max = b; } int main() { int num1 = 0; int...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using...

    My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using namespace std; int main() {    int n;    int flag= 1;    cout << "Prime/Not Prime" << endl;    cout << "Enter the Number to check Prime:" << endl;    cin >> n;    int m = n / 2;    int i;    for (i = 2; i <= m; i++)        if (n % i == 0)           ...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • (C++) What is the best way to validate for the two user inputs in the followig...

    (C++) What is the best way to validate for the two user inputs in the followig for loop? Would like it to say "That value is not allowed. Enter another value." , if 1 ) numbers are not within range 2) a blank is entered 3) other characters (that are not numbers) are entered and 4) numbers with decimals or negatives are entered. int num1; int num2; for(int i = 1; i <= 4; i++) { cout << "Enter number...

  • C++ language I need to update this code with the following: ask the user for how...

    C++ language I need to update this code with the following: ask the user for how many structures they would like. Once you get the number, allocate an array of pointers. Once they have been created, proceed to loop through and get the data as usual, and display it back to the screen. struct record {    int age;    string name; }; int check(record r[], int n, string nm) {    for (int i = 0; i < n;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT