Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:47:58+00:00 2026-05-30T03:47:58+00:00

How do I use a file called dados.txt as input data for multiplication of

  • 0

How do I use a file called dados.txt as input data for multiplication of two arrays?
That is, I do not want to use cin, but read the data file dados.txt

#include <iostream>
#include <fstream>
#define MAX 100
using namespace std;
int main()
{
    int A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
    int m, n, p, i, j, k, aux;
    ifstream arquivo;
    arquivo.open("dados.txt");
    while (arquivo >> aux)
    {
        //cin >> m >> n >> p;
    //read dados.txt
        for (i = 0; i < m; i++)
            for (j = 0; j < n; j++)
                cin >> A[i][j];
        for (i = 0; i < n; i++)
            for (j = 0; j < p; j++)
                cin >> B[i][j];
        for (i = 0; i < m; i++)
            for (j = 0; j < p; j++)
            {
                C[i][j] = 0;
                for (k = 0; k < n; k++)
                    C[i][j] += A[i][k] * B[k][j];
            }
        for (i = 0; i < m; i++)
        {
            for (j = 0; j < p; j++)
                cout << C[i][j] << " ";
            cout << endl;
        }
    }
    arquivo.close();
    return 0;
}

dados.txt The file contains the following data (example):

3 5 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:48:00+00:00Added an answer on May 30, 2026 at 3:48 am

    You’re already reading from the file through the filestream that you declared, arquivo. Instead of doing cin >> A[i][j], you just do arquivo >> A[i][j]. But you also need to take out arquivo >> aux from the while condition. The way you have it setup, it seems you know exactly how long the loop will be. You don’t really even need the while loop. That part of the code can just be:

    arquivo.open("dados.txt");
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            arquivo >> A[i][j];
    for (i = 0; i < n; i++)
        for (j = 0; j < p; j++)
            arquivo >> B[i][j];
    for (i = 0; i < m; i++)
        for (j = 0; j < p; j++)
        {
            C[i][j] = 0;
            for (k = 0; k < n; k++)
                C[i][j] += A[i][k] * B[k][j];
        }
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < p; j++)
            cout << C[i][j] << " ";
        cout << endl;
    }
    arquivo.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have come across a website that appears to use Ajax but does not
In my bat script, what do I use to open a file called open.txt
I have a file called something like FILE-1.txt or FILE-340.txt. I want to be
I have custom checkstyle checks file (called checks.xml), and I'm trying to use that
I have a file called myConstants.h that I use to put all the constants
I have a file called one_to_many.txt. In the file is the data: a,aaa b,bbb
Write a program which reads a text file called input.txt which contains an arbitrary
In bash I have a file called temporary.txt that contains one line of text.
I have developed a php file called profile.php that displays the current user's profile.But,
I use this tool called Lazy C++ which breaks a single C++ .lzz file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.