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 784315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:41:36+00:00 2026-05-14T20:41:36+00:00

I am a very basic user and do not know much about commands used

  • 0

I am a very basic user and do not know much about commands used in C, so please bear with me…I cant use very complicated codes. I have some knowledge in the stdio.h and ctype.h library, but thats about it.
I have a matrix in a txt file and I want to load the matrix based on my input of number of rows and columns

For example, I have a 5 by 5 matrix in the file. I want to extract a specific 2 by 2 submatrix, how can I do that ?

I created a nested loop using :

FILE *sample
sample=fopen("randomfile.txt","r"); 
for(i=0;i<rows;i++){
  for(j=0;j<cols;j++){
     fscanf(sample,"%f",&matrix[i][j]);
   }
 fscanf(sample,"\n",&matrix[i][j]);
}
fclose(sample);

Sadly the code does not work .. If I have this matrix :

5.00 4.00 5.00 6.00 
5.00 4.00 3.00 25.00 
5.00 3.00 4.00 23.00 
5.00 2.00 352.00 6.00

And inputting 3 for row and 3 for column, I get :

5.00 4.00 5.00
6.00 5.00 4.00
3.00 25.00 5.00

Not only this isnt a 2 by 2 submatrix, but even if I wanted the first 3 rows and first 3 columns, its not printing it correctly….

I need to start at row 3 and col 3, then take the 2 by 2 submatrix !

I should have ended up with :

4.00 23.00 
352.00 6.00

I heard that I can use fgets and sscanf to accomplish this. Here is my trial code :

fgets(garbage,1,fin);
sscanf(garbage,"\n");

But this doesnt work either 🙁

What am I doing wrong ?

Please help.
Thanks !

  • 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-14T20:41:37+00:00Added an answer on May 14, 2026 at 8:41 pm

    OK, so you want to read a submatrix of size n x m, starting at positions x, y in the big matrix of size p x q. You need two things:

    1. (verify that x + n <= p and y + m <= q)
    2. skip to the first element of the matrix you want to read. This requires first skipping the first y – 1 rows
    3. skip x – 1 elements from the next row, then read n elements into your submatrix. Repeat m times.

    Your current implementation starts reading from the very first element of the matrix, then reads elements contiguously into the submatrix. An updated version:

    FILE *sample = fopen("randomfile.txt", "r");
    // skip the first y-1 rows
    for (i = 0; i < y - 1; i++) {
      fscanf(sample, "%*[^\n]\n", &matrix[i][j]);
    }
    for (i = 0; i < m; i++) {
      // skip the first x-1 numbers
      for (j = 0; j < x - 1; j++) {
         fscanf(sample, "%*f");
      }
      // read n numbers
      for (j = 0; j < n; j++) {
         fscanf(sample, "%f", &matrix[i][j]);
      }
      if (x + n < p) {
        // consume the rest of the line
        fscanf(sample, "%*[^\n]\n");
      }
    }
    fclose(sample);
    

    Update: to read the submatrix from an array instead is even simpler, just requires a bit more calculation. The gist is, a matrix of size p x q can be stored in a contiguous array of size p x q such that matrix[i,j] can be read from array[i*(j-1)+j] (approximately – there may be off-by-one errors and I am never sure which is the column and which is the row, but hopefully you get the idea 🙂

    So the code would be something like

    for (i = 0; i < m; i++) {
      for (j = 0; j < n; j++) {
         submatrix[i][j] = array[(y + i) * p + x + j];
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 414k
  • Answers 414k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, there are several libraries that support Android. However, you… May 15, 2026 at 8:46 am
  • Editorial Team
    Editorial Team added an answer I would first take the approach of fixing the broken… May 15, 2026 at 8:46 am
  • Editorial Team
    Editorial Team added an answer This is not readily available, you'd have to P/Invoke GetWindowPlacement.… May 15, 2026 at 8:46 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.