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
  • 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...

  • 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...

  • 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++ 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;...

  • (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...

  • in c++ language 1.Re-write the following for loop statement by using a while loop statement: int...

    in c++ language 1.Re-write the following for loop statement by using a while loop statement: int sum = 0; for(int i=0;i<=1000;i++){                 sum+=i; } 1 (b). Following is the main () function of a program. The program asks a user to enter 150 integers and print the largest integer user entered. int main() {    int score, highest;             //missing portion of the program             return 0;    } 1(c). Find the missing portion of the following code, so the...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

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
ADVERTISEMENT