Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program which shows content of a given 2D array

Posted By: Easy Tutor     Category: C++ Programming     Views: 5039

Make a program which shows content of a given 2D array using the following criteria.

The input will contain multiple test cases and will end on the line containing 0. The size of the 2D array will be mxn where 0<m<25 and 0<n<25.

Code for Program which shows content of a given 2D array in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-13.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"Unable to open the input file."<<endl;
         cout<<"Press any key to exit."<<endl;

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80,'\n');

         if(strcmpi(Data,"0")==0)
        break;

         char *Ptr=NULL;

         int rows=0;
         int columns=0;

         Ptr=strtok(Data," ");
         rows=atoi(Ptr);

         Ptr=NULL;
         Ptr=strtok(NULL,"\n");
         columns=atoi(Ptr);

         char Array2d[50][50]={NULL};

         for(int count_1=0;count_1<rows;count_1++)
        File.getline(Array2d[count_1],80);

         int direction=0;

         do
        {
           char Temp[50][50]={NULL};

           if(direction==0)
              {
             cout<<Array2d[0];

             for(int count_2=1;count_2<rows;count_2++)
                strcpy(Temp[(count_2-1)],Array2d[count_2]);

             rows--;
              }

           elseif(direction==1)
              {
             for(int count_3=0;count_3<rows;count_3++)
                {
                   cout<<Array2d[count_3][(columns-1)];

                   Array2d[count_3][(columns-1)]=NULL;
                }

             for(int count_4=0;count_4<rows;count_4++)
                strcpy(Temp[count_4],Array2d[count_4]);

             columns--;
              }

           elseif(direction==2)
              {
             cout<<strrev(Array2d[(rows-1)]);

             for(int count_5=0;count_5<(rows-1);count_5++)
                strcpy(Temp[count_5],Array2d[count_5]);

             rows--;
              }

           elseif(direction==3)
              {
             for(int count_6=(rows-1);count_6>=0;count_6--)
                cout<<Array2d[count_6][0];

             for(int count_7=0;count_7<rows;count_7++)
                {
                   for(int count_8=1;count_8<columns;count_8++)
                  Temp[count_7][(count_8-1)]=Array2d[count_7][count_8];
                }

             columns--;
              }

           direction++;

           if(direction==4)
              direction=0;

           for(int count_9=0;count_9<50;count_9++)
              {
             strset(Array2d[count_9],NULL);
             strcpy(Array2d[count_9],Temp[count_9]);
              }

           if(rows==0)
              break;
        }
         while(1);

         cout<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }
  
Share: 


Didn't find what you were looking for? Find more on Program which shows content of a given 2D array Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program which shows content of a given 2D array is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!