Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
/****************************
Title : PROJECT 
Programmer: CALCULATION LEVELLING USING LEAST SQUARE ADJUSTMENT
Email: 
FIle Name: aiman
Date:28/12/2020
****************************/

#include<iostream>  // when use function cin, cout
#include<fstream>   // when use file stream function fstream or ofstream
#include <cmath>	// when used function such as sin, cos, tan, power bagai kne pkai library ni.
#include <iomanip>  // when use function such as setprecision, fixed etc
#include <string>

using namespace std;	//

int main()             // main function
{
    //declaration
    ifstream fp1;
    ofstream fp2;
    char inputFile[50], outputFile[50], ans, Hx[10], Hy[10], Hz[10], HDiff[20], HDist[20], name1[10], name2[10], name3[10], name4[10], name5[10]; 
    char name6[10], name7[10],name8[10],name9[10],name10[10],name11[10], name[10], Obs, From, To, Date[20], Rl[20], RlB[10], RlC[10], x[10],y[10],z[10];
    char v1,v2,v3,h1,h2,h3,A,B,C;
    char dF,dx,dG,dH;
    double Diff[10], Dist[10],X[20];
    int i,j,n,k,c[10][10],d[10][10],e[10][10],f[10][10],transpose[10][10],r1=3,c1=2,r2=3,c2=3,r3=2,c3=1;
    int a[3][2] = { {1, 0} , {0, 1}, {-1, 1} };
    int W[3][3] = { {10, 0, 0} ,{0, 10, 0}, {0, 0, 10} };
    double L[3][1] = { {99.25}, {105.46} , {3.73} };
        
//user enter file name
    again:
    cout << "Please Enter Input Filename: ";
    cin >> inputFile;
    
//protocol read file
    
    fp1.open(inputFile);
    if(!fp1)
    {
        cout << "Input File " << inputFile << " Opss sorry cannot be opened.";
        cout << "\nDo You Like to Try Again? (Y/N)";
        cin >> ans;
        if(ans == 'y' || 'Y') goto again;
        return 0;
    }
    cout << "\nInput File " << inputFile << " readed successfully.";
    
    //program read data input file
    fp1 >> Date;                //start counting data
    fp1 >> name >> name1 >> RlB >> name2 >> RlC;
    fp1 >> name3 >> name4 >> name5 >> Rl;
    fp1 >> name6 >> name7; 
    fp1 >> name8 >> name9 >> name10 >> name11;
    fp1 >> Hx >> Hy >> Hz >> HDiff >> HDist;
    for(i=1; i<=3; i++)                  
        {
            fp1 >> x[i] >> y[i] >> z[i] >> Diff[i] >> Dist[i];   //counting data                                             
        }
    //Output read file
    cout << endl ; 
    cout << "\n" << Date;
    cout << "\n" << name << "\n" << "\n" << name1  << "\t" << RlB <<"\t" << name2 << "\t" << RlC;
    cout << "\n" << name3 << "\t" << name4 << "\t" << name5 << "\t" << Rl << "\t" << name6;
    cout << "\n" << "\n" << name7 << "\t" << name8 << "\t" << name9;
    cout << "\t" << name10 << "\t" << name11;
    cout << "\n" << Hx << "\t" <<  Hy << "\t" << Hz << "\t" << HDiff << "\t" << "\t" << HDist;
    for (i =1; i<3; i++)
        {
            cout << "\n" << x[i] << "\t" << y[i] << "\t" << z[i] << "\t" << Diff[i] << "\t" << "\t" << Dist[i];
        }
    
    //Proses Data
    //Create the basic formula
    //Create the observation equation in form of V=AX-L,

    v1=B-A-h1;
    v2=C-A-h2;
    v3=C-B-h3;
    
    cout << "\n" << "\n" << "v1=B-A-h1" << "\t" << "v1 = B - 100.25 - (-1.00)" << "\t" << "v1=B-99.25" <<endl;
    cout << "\n" << "v2=C-A-h2" << "\t" << "v2 = C - 100.25 - 5.21" << "\t\t" << "v2=C-105.46"<<endl;
    cout << "\n" << "v3=C-B-h3" << "\t" << "v3 = C - B - 3.73" << "\t\t" << "v1=C-B-3.73"<<endl;
    
    
    //Create the uknown matrix for RL B and RL C
    
    cout << "\nmatrix X";
      string m_X[2] = {"\nX =\tB","\tC"};
      
   for (i = 0; i<2; i++) {
         cout << m_X[i] << endl; 
         
   }
    
    //Create the coefficient matrix
      cout<<"\nmatrix A:"<<endl;
 
      for(i=0; i<r1; i++) {  
        for(j=0; j<c1; j++)
         cout<<a[i][j]<<" ";
      cout<<endl;
   }
      cout<<endl;
         for(i=0; i<r1; i++)
            for(j=0; j<c1; j++) {
        transpose[j][i] = a[i][j];
      }
      cout<<"\nTranspose A:"<<endl;
   for(i=0; i<c1; i++) {  
      for(j=0; j<r1; j++)
         cout<<transpose[i][j]<<" ";
      cout<<endl;
   }
    //Create L matrix
      L[i][j] = 0.0;
      cout << "\n\nmatrix L: "<<endl;
        for (i=0; i<r1; i++) {
            for (j=0; j<c3; j++) {
                cout << L[i][j]<<" ";
                cout<<endl;
            }
        }
    
    //Create W matrix
    
    cout << "\n\nmatrix W: "<<endl;
      
        for(i=0; i<r2; i++) {  
            for(j=0; j<c2; j++)
         cout<< W[i][j]<<" ";
      cout<<endl;
   }
    
    //Calculate RLB and RLC
    
   cout<<endl;
   for(i=0; i<r1; i++)
      for(j=0; j<c1; j++) {
         c[i][j] = 0;
      }
      for(i=0; i<r1; i++)
         for(j=0; j<c1; j++)
            for(k=0; k<c2; k++) {
               c[i][j]+=W[i][k]*a[k][j];  
            }
            
    cout<<"RLB AND RLC:"<<endl;
    cout<<"(A^tWA):"<<endl;
        for(i=0; i<r1; i++) {   
            for(j=0; j<c1; j++)
            cout<<c[i][j]<<" ";
            cout<<endl;
            }


What I have tried:

i try to multiple the matrix 3x3 with 3x2 but the asnwer is wrong please help me
Posted
Updated 16-Jan-21 3:35am
v5
Comments
Rick York 15-Jan-21 0:50am    
I edited your post to remove your ID number. It's probably not a good idea to advertise that.

Not easy to go through your entire code shared.

High level:
1. Make sure two matrix can be multiplied
2. Store elements of first & second matrix A & B
3. Initialize elements of result matrix C to 0
4. Multiply matrix A and B and store in C
5. Print C

Refer: C++ Program to Multiply Two Matrix Using Multi-dimensional Arrays[^]

Try:
C++
#include <iostream>
using namespace std;

int main()
{
    int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

    cout << "Enter rows and columns for first matrix: ";
    cin >> r1 >> c1;
    cout << "Enter rows and columns for second matrix: ";
    cin >> r2 >> c2;

    // If column of first matrix in not equal to row of second matrix,
    // ask the user to enter the size of matrix again.
    while (c1!=r2)
    {
        cout << "Error! column of first matrix not equal to row of second.";

        cout << "Enter rows and columns for first matrix: ";
        cin >> r1 >> c1;

        cout << "Enter rows and columns for second matrix: ";
        cin >> r2 >> c2;
    }

    // Storing elements of first matrix.
    cout << endl << "Enter elements of matrix 1:" << endl;
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c1; ++j)
        {
            cout << "Enter element a" << i + 1 << j + 1 << " : ";
            cin >> a[i][j];
        }

    // Storing elements of second matrix.
    cout << endl << "Enter elements of matrix 2:" << endl;
    for(i = 0; i < r2; ++i)
        for(j = 0; j < c2; ++j)
        {
            cout << "Enter element b" << i + 1 << j + 1 << " : ";
            cin >> b[i][j];
        }

    // Initializing elements of matrix mult to 0.
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
        {
            mult[i][j]=0;
        }

    // Multiplying matrix a and b and storing in array mult.
    for(i = 0; i < r1; ++i)
        for(j = 0; j < c2; ++j)
            for(k = 0; k < c1; ++k)
            {
                mult[i][j] += a[i][k] * b[k][j];
            }

    // Displaying the multiplication of two matrix.
    cout << endl << "Output Matrix: " << endl;
    for(i = 0; i < r1; ++i)
    for(j = 0; j < c2; ++j)
    {
        cout << " " << mult[i][j];
        if(j == c2-1)
            cout << endl;
    }

    return 0;
}
/*
Output
Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:
Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24 29
6  25
*/

If you have followed above, do a quick debug to figure where things went wrong. First your algo and then the code - locate the error and fix it.
 
Share this answer
 
v2
Comments
CPallini 15-Jan-21 3:08am    
5.
Sandeep Mewara 15-Jan-21 4:48am    
Thanks Carlo!
Quote:
i try to multiple the matrix 3x3 with 3x2 but the asnwer is wrong please help me

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900