Thread: small problems .. need help ..

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    9

    Unhappy small problems .. need help ..

    Hi All ..

    I'm having small problems with this program ..

    The program suppose to print :

    1. Main Diagonal Elements and its Sum .

    2. Inverse Diagonal Elements and its Sum .

    3. Max and Min element and their positions .

    4. The Elements Upper the Main Diagonal .

    5. The Elements Under the Main Diagonal .


    This is the code :

    Code:
    #include <iostream>
    #include <conio>
    #include <iomanip>
    
    int main()
    {
    int A[5][5]={{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15},{16,17,18,19,20},{21,22,23,24,25}};
    int i , j ,  sum=0 , r_n=0 , c_n =0 , r_m = 0 , c_m = 0 , up=0 , low=0;
    int min = A[0][0];
    int max = A[0][0];
    
    
    cout<< "Array Elements Are" << endl;
    for (i=0;i<5;i++)
    { for (j=0;j<5;j++)
       { cout<<  setw(5) << A[i][j];
       }
       cout<< "\n";
    }
    
    for (i=0;i<5;i++)
    { for (j=0;j<5;j++)
             if (i==j)
        {
               sum+=A[i][j];
               cout<< "Main Diagonal Elements are : " << A[i][j] << endl;
        }
    
    }
    
    for (i=0;i<5;i++)
    { for (j=0;j<5;j++)
         { if (A[i][j] > max)
         {
         max= A[i][j];
         r_m= i;
         c_m= j;
         }
         if (A[i][j]< min)
         {
         min=A[i][j];
         r_n=i;
         c_n=j;
         }
         }
    }
    
    for (i=0;i<5;i++)
    {  for (j=0;j<5;j++)
           {     if ((i!=j) && (j > i) )
              {
               up+=A[i][j];
               cout<< "The upper Elements are : " <<  A[i][j]<< endl;
              }
           {
              if (j<i)
             {
             low+=A[i][j];
             cout<< "The Lower Elements are : "<< A[i][j]<< endl;
             }
             }
          }
    }
    
    
    cout<< "\n\nSum of Main Diagonal Elements is : " << sum;
    cout<< endl << "The Minimum Element =" << min << " At Row " << (r_n+1) << " Col " << (c_n+1);
    cout<< endl << "The Maximum Element =" << max << " At Row " << (r_m+1) << " Col " << (c_m+1);
    cout<< "\nSum of Upper Elements : " << up << endl;
    cout<< "\nSum of Lower Elements : " << low << endl;
    
    getch();
    return 0;
    }
    __________________________

    The FIRTS PROBLEM is in this loop :

    Code:
    for (i=0;i<5;i++)
    { for (j=0;j<5;j++)
             if (i==j)
        {
               sum+=A[i][j];
               cout<< "Main Diagonal Elements are : " << A[i][j] << endl;
        }
    
    }
    The OUTPUT is :

    Code:
    Main Diagonal Elements are : 1
    Main Diagonal Elements are : 7
    Main Diagonal Elements are : 13
    Main Diagonal Elements are : 19
    Main Diagonal Elements are : 25
    it will repeat the statements : Main Diagonal ......

    How can I fix it ??

    it's the same problem in upper , lower Elements ..

    I know it's noob problem but I really don't know how to fix it ><" ...

    _________________________

    The SECOND PROBLEM is I dunno how to print the INVERSE DIAGONAL ...

    Please help me even with small explanation ..

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    It's because you have the cout statement within the for loop, so each time through it is printing the statement.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    And for the inverse diagonal I would imagine it's simply a case of reversing the for loops? I'm not an expert so maybe somebody else can explain better.

    Maybe something along these lines:

    Code:
    for(j = 4; j > 0; j--){
    	for (i = 0; i < 5; i++){
    		cout << A[i][j] << " ";
                                    j--;
    		
    	}
    }
    Last edited by darren78; 11-13-2009 at 03:01 AM. Reason: New info

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The FIRTS PROBLEM is in this loop :
    Code:
    for (i=0;i<5;i++)
    { for (j=0;j<5;j++)
             if (i==j)
        {
               sum+=A[i][j];
               cout<< "Main Diagonal Elements are : " << A[i][j] << endl;
        }
    
    }
    The OUTPUT is :

    Code:
    Main Diagonal Elements are : 1
    Main Diagonal Elements are : 7
    Main Diagonal Elements are : 13
    Main Diagonal Elements are : 19
    Main Diagonal Elements are : 25
    it will repeat the statements : Main Diagonal ......

    How can I fix it ??
    The problem is the repeated output of "Main Diagonal"? Then don't put that part of the output in the loop. Put it once before the loop like the prior code that displays all the array elements.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    9
    THX 4 your Help .. IT worked

    still the Inverse Diagonal .. if there is any help !!

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Kawaii JoJo View Post
    THX 4 your Help .. IT worked

    still the Inverse Diagonal .. if there is any help !!
    Look at my code above, I put something there that should help. I don't think it's perfect though as i'm learning as well, but it works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. openGL problems (glut)
    By c_young in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2007, 01:27 PM
  4. Small app ideas
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 02-15-2002, 08:57 PM
  5. 2 small problems
    By Robert_Ingleby in forum Windows Programming
    Replies: 3
    Last Post: 11-21-2001, 06:16 AM