Sireiz
Sireiz

Reputation: 393

Having a Runtime error in this code

I am having a run-time error in this program, it has no syntax error but crashes when it is run. i am using dev-c++ 4.9.9.2. I tried to find the error but i couldn't find it. If anyone can help then please find the errors and correct me.

#include<iostream.h>


void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();

main()
{
      DisplayVUID();
      char a[20] = "mc123456789";
      DisplayReverse(a, 20 );
      StoreDiagonal();

system("pause");
}
void DisplayVUID()
{
     int i;
     char name[20] = "mc123456789";
     cout<<"My VU id is ";
     for(i=0;i<20;i++)
     {
          cout<<name[i];
     }
     cout<<endl;
}
void DisplayReverse(char a[], int arraysize)
{
     int i;
     cout<<"MY VU id in Reverse is ";
     for(i=arraysize-1;i>=0;i--)
     {
       cout<<a[i];
     }
     cout<<endl;                           
}
void StoreDiagonal()
{
     int a[9][9] ;
     int i;
     int row, col;
     for (i = 0; i<9;i++)
     {
         for(i=0;i<9;i++)
         {
         a[row][col] = 0;
         }
     }
a[1][1] = 1;
a[2][2] = 3;
a[3][3] = 0;
a[4][4] = 2;
a[5][5] = 0;
a[6][6] = 2;
a[7][7] = 3;
a[8][8] = 9;
a[9][9] = 8;
      for(i = 0 ; i < 9 ; i ++)
              {
                for( i = 0 ; i < 9 ; i ++)
                {
                cout<<a[row][col];
                }
              }
}

Upvotes: 0

Views: 115

Answers (2)

0decimal0
0decimal0

Reputation: 3984

Things don't work this way on Stackoverflow, from next time onward try hard to do things on your own, do your research and then come here.There seemed to be many errors in your program but I tried to remove some bugs and finally, It works on my system. I have also recommended some nice things via comments that you can look in the program:

EDIT: I noticed that some undefined string because of unassigned spaces in the array was printed out in the reverse function but i have corrected it now.

#include<iostream>
#include<stdlib.h>
using namespace std;// use namespace otherwise cout won't work


void DisplayVUID();
void DisplayReverse(char[], int);
void StoreDiagonal();

int main()// In C++ always declare a main function like this,its good habit
{
      int i=0;
      DisplayVUID();
      char a[20] = "mc123456789";
      while(a[i]!='\0')
       i++;// Did this to ensure that cout will print only up to the null character,earlier it was printing some undefined characters along with the data in the array.
      DisplayReverse(a, i );
      StoreDiagonal();

  system("pause");
  return 0;//return 0
}
void DisplayVUID()
{
  //int i;
     char name[20] = "mc123456789";
     cout<<"My VU id is ";
     //for(i=0;i<20;i++)// no need for this loop at least here
     //{
          cout<<name;
          //}
     cout<<endl;
}
void DisplayReverse(char a[], int i)
{
     cout<<"MY VU id in Reverse is ";
   //  for(i=arraysize-1;i>=0;i--)
     //{
       while(i--)
       cout<<a[i];//instead of the for loop traversing for the whole arraysize i have made it go up to only the null terminator this way it doesn't print undefined characters.
     //}
     cout<<endl;                           
}
void StoreDiagonal()// i don't understand by the way what this function is here for , its not an error though.
{
     int a[9][9] ;
     int i,j,c=1;
     //int row, col;// you didn't initialize row and column and placed it inside the loop 
     for (i = 0; i<9;i++)
     {
         for(j=0;j<9;j++)
         {
         a[i][j] = 0;
      if(i==j)
       {
       a[i][j]=c;//instead of manually assigning do it like this.
       c++;
       }

         }
     }
       for(i = 0 ; i < 9 ; i ++)
              {
                for( j = 0 ; j < 9 ; j ++)
                {

                  cout<<a[i][j];// the elements of the array don't display like a table , this is not an error but to make your output readable do it by your self

              }
              }
}

Upvotes: 4

banarun
banarun

Reputation: 2321

a[9][9]=8;

remove this line, you will be fine. Array indexing starts from 0 not 1.

Also I would like to point that in your function DisplayVUID() change i<20 to a[i]!='\0' because the values after '\0' will be garbage values.

Upvotes: 2

Related Questions