How do I make this program work promptly. For some reasonafter it tells me to input the 10 digits, it just closes out anddoesnt run to give me an output. Why does the program does notcontinue to run, and also my teacher specified that it should printout each element in the array, does this program do so? Also hesaid that I can add additional functions such as printing a twodimensional array. If you can help me revise the program so that itworks, please help.

#include<iostream>
using namespace std;
void setZero(int [],int);
void inputAlpha (int [], int);
void doubleArray (int [], const int [], int);
void copyGamma (int [] [4], const int []);
void copyAlphaBeta (int [] [4], const int [], const int []);
void printArray (const int [], int);
void setInStock (int [] [4], const int []);
void printArray2 (const int [][4], int, int);
int main()
{
    int inStock [10] [4];
    int alpha [20];
    int beta[20];
    int gamma[4] = {11, 13, 15, 17};
    int delta[10] = {3, 5, 2, 6, 10, 9, 7, 11, 1,8};
   
    setZero (alpha, 20);
    setZero (beta, 20);
   
    inputAlpha (alpha, 20);
    printArray (alpha, 20);
    copyGamma (inStock, gamma);
    printArray2 (inStock, 10, 4);
    copyAlphaBeta (inStock, alpha, beta);
    printArray2 (inStock, 10, 4);
    setInStock (inStock, delta);
    printArray2 (inStock, 10, 4);
   
   
    return 0;
}
void setZero (int myArray[], int size)
{
     for(int i=0; i<size; i++)
            myArray[i] = 0;
}
void inputAlpha (int myAlpha [], int size )
{
     cout <<"\n\       Press the 'ENTER' keyafter each number"<<endl;
     cout <<"\n\tEnter a total of "<<size << " numbers: "<<endl;
    
     for (int i =0; i < size; i++)
         cin>>myAlpha [i];
}
void doubleArray (int myBeta[], const int myAlpha[], int size)
{
     for( int i =0; i < size; i++)
          myBeta[i] =2 * myAlpha [i];
}
void copyGamma ( int myStock[][4], const int myGamma[])
{
     for (int i =0; i < 4; i++)
         myStock[0][i] =myGamma[i];
        
     for( int i = 0; i < 10; i++)
          for (int j =0; j < 4; j++)
             myStock[i][j] = 3 * myStock[i-1][j];
}
void copyAlphaBeta ( int myStock[][4], const int myAlpha[], constint myBeta[])
{
     for(int i = 0; i < 20; i++)
            myStock[i/4][i%4] = myBeta[i];
}
void printArray (const int myArray[], int size )
{
     int i = 0;
     while (i < size)
     {
           cout<< myArray[i]<< ", ";
          i++;
          
           if(i%15 == 0)
             cout << endl;
      }
}
void setInStock (int myStock[][4], const int myDelta[])
{
     cout <<"\n\n\tEnter (10) elementsinto first column "<<endl;
    
     for (int i =0; i< 10; i++)
         cin >>myStock [i][0];
        
         for (int i = 0; i<10; i++)
            for (int j =1; j < 4; j++)
                myStock[i][j] = 2 * myStock[i][j-1] - myDelta[i];
}
void printArray2 (const int myArray[][4], int r, int c)
{
     for(int i = 0; i < r; i++)
     {
            for(int j = 0; j < c; j++)
                    cout << myArray[i][j] << "\t";
            cout << endl;
     }
}

Share:
by email on facebook on twitter

Ezzaral commented: Utter fail. -3

Dani AI

Generated

The most likely cause of the program exiting unexpectedly is an out‑of‑bounds access in the routines that build the 2D array. In the posted code the loop that fills rows from the previous row reads myStock[i-1][j] even when i == 0, which accesses memory before the array and leads to undefined behavior (the usual symptom is the program closing with no output). There are also a couple of smaller issues worth noting: inputAlpha requests 20 values while the OP described entering 10 (mismatched expectations), and a typographical parameter error (constint) appears in the posted copyAlphaBeta signature and would fail to compile if present.

Minimal, targeted fixes

  • Start the dependent fill loop at row 1 (not 0) so i-1 is valid.
  • Fix function signatures (add the missing space in const int) and make explicit which source array is intended when copying 20 values into the 10x4 table.
  • Make the print routine print one element per line (or show index/value pairs) if the instructor expects each element on its own line.

Example corrected fragments:

void copyGamma(int myStock[][4], const int myGamma[])
{
    for (int j = 0; j < 4; ++j)
        myStock[0][j] = myGamma[j];

    for (int i = 1; i < 10; ++i)
        for (int j = 0; j < 4; ++j)
            myStock[i][j] = 3 * myStock[i-1][j];
}
void printArray(const int a[], int size)
{
    for (int i = 0; i < size; ++i)
        std::cout << "a[" << i << "] = " << a[i] << '\n';
}

Debugging and hardening tips

  • Compile with warnings and sanitizers: g++ -std=c++17 -Wall -Wextra -fsanitize=address,undefined -O0.
  • Use asserts or bounds-checked loops, or prefer std::array/std::vector with .at() while debugging.
  • Run under a debugger or Valgrind to catch invalid reads/writes.

These targeted changes remove the invalid negative index access and align printing with the teacher's requirement. Echoing ’s point, make incremental edits and recompile after each change so the exact failure point is easy to identify.

Well, bunny09 (that's your name on Cramster, right?), perhaps you should make an attempt to write your own code for your homework assignment and then you would know what the program would or would not do.

Edit: Hmm, no apparently 'bunny09' is just the user who posted this question in it's entirety in 2009 and you pasted it here? What exactly are you trying to accomplish?

commented: Good detective work! +7
commented: 'That's what she said jonsca" .. but no, really. Good job. +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.