1-dimensional array type double.....and....2-dimensional array

Discussion in 'C++' started by styles8687, Dec 2, 2007.

  1. styles8687

    styles8687 New Member

    Joined:
    Dec 2, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Illinois
    ok i have to write two 1-dimensional arrays and one 2-dimensional array, but i was out of town so i have no idea of what im doin. any help i can get with explaining it would be greatly appreciated.

    1-dimensional:
    write an interactive C++program that asks the user to enter the total rainfall for each of 12 months into a one dimensional array of type double. The program then calculates and displays the total rainfall for the year and the months with the highest and lowest amount. do not accept negative numbers for monthly rainfall...use a loop. if any1 is able 2 explain this 2 me that would b awesome.


    2-dimensional:
    program should....declare and initialize an array called values, print resultant array, determine and print sum of each row, determine and print sum of each column, determine and print sum of each diagonal. must use loop to accomplish this.
     
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Joined:
    Jul 18, 2008
    Messages:
    101
    Likes Received:
    0
    Trophy Points:
    0
    How far have you got? Where are you stuck?
     
  3. hkp819

    hkp819 New Member

    Joined:
    Dec 4, 2008
    Messages:
    59
    Likes Received:
    1
    Trophy Points:
    0
    This program takes in the number of rows (n) and columns (m) as well as the elements as a screen input in a matrix n x m.
    It then calculates the sum of each row and each column and outputs it using the 'cout' command.
    Also, if it is a square matrix, it calculates the sum of diagonal elements and prints it out.

    I do not understand clearly your question if you tell for diagonal sum of matrix(2-dimensional array) than program is below....
    In this program user enter the row and columns and value of matrix
    Than output is sum of diagonal elements.
    Code:
    #include <iostream.h>
    #include <conio.h>
    int main()
    {
    clrscr();
    int A[10][10],m,n,x,y,sum=0;
    //Create a Matrix A
    cout << "Enter number of rows and columns in Matrix A : \n";
    cin>>n>>m;
    cout << "Enter elements of Matrix A : \n";
    for(x=1;x<n+1;++x)
    for(y=1;y<m+1;++y)
    cin>>A[x][y];
    //Find sum of each row
    for(x=1;x<n+1;++x)
    {
    A[x][m+1]=0;
    for(y=1;y<m+1;++y)
    A[x][m+1]=A[x][m+1]+A[x][y];
    }
    //Find sum of each column
    for(y=1;y<m+1;++y)
    {
    A[n+1][y]=0;
    for(x=1;x<n+1;++x)
    A[n+1][y]+=A[x][y];
    }
    cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
    for(x=1;x<n+1;++x)
    {
    for(y=1;y<m+2;++y)
    cout << A[x][y] << "     ";
    cout << "\n";
    }
    //Print sum of each column
    x=n+1;
    for(y=1;y<m+1;++y)
    cout << A[x][y] << "     ";
    cout << "\n";
    if(m==n)
    {
    for(x=1;x<m+1;x++)
    for(y=1;y<n+1;y++)
    if(x==y)
    sum+=A[x][y];
    else
    if(y==m-(x+1))
    sum+=A[x][y];
    }
    cout << "Sum of diagonal elements is : " << sum << endl;
    getch();
    return 0;
    }

    Input by the user:

    3 3
    9 8 7 6 5 4 3 2 1

    output of that input

    Matrix A, Row Sum(Last Column) and Column Sum(Last Row) :
    9 8 7 24
    6 5 4 15
    3 2 1 6
    18 15 12
    Sum of diagonal elements is : 15
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice