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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:46:57+00:00 2026-06-13T05:46:57+00:00

I am trying to write 2 functions, one to read the matrix (2D array)

  • 0

I am trying to write 2 functions, one to read the matrix (2D array) and other one to print it out. So far I have:

    /* Read a matrix: allocate space, read elements, return pointer. The
       number of rows and columns are given by the two arguments. */
    double **read_matrix(int rows, int cols){

        double **mat = (double **) malloc(sizeof(double *)*rows);
        int i=0;
        for(i=0; i<rows; i++){
        /* Allocate array, store pointer  */
        mat[i] = (double *) malloc(sizeof(double)*cols); 
       //what to do after??

         return mat;
    }

then the print matrix function, not sure if it is correct

void print_matrix(int rows, int cols, double **mat){
    for(i=0; i<rows; i++){    /* Iterate of each row */
      for(j=0; j<cols; j++){  /* In each row, go over each col element  */
      printf("%f ",mat[i][j]); /* Print each row element */
  }
}
}

and here is the main function I am using to run:

    #include <stdlib.h>


    double **read_matrix(int rows, int cols);
    void print_matrix(int rows, int cols, double **mat);
    void free_matrix(int rows, double **mat);

    int main(){

      double **matrix;
      int rows, cols;
      /* First matrix */
      printf("Matrix 1\n");
      printf("Enter # of rows and cols: ");
      scanf("%d %d",&rows,&cols);
      printf("Matrix, enter %d reals: ",rows*cols);
      matrix = read_matrix(rows,cols); 
      printf("Your Matrix\n");  /* Print the entered data */
      print_matrix(rows,cols,matrix);
      free_matrix(rows, matrix);   /* Free the matrix */

        return 0;
}
  • 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-06-13T05:46:58+00:00Added an answer on June 13, 2026 at 5:46 am

    Try this one. may be helpful to you.

    #include <stdio.h>
    #include <stdlib.h>
    
    double **read_matrix(int rows, int cols);
    void print_matrix(int rows, int cols, double **mat);
    void free_matrix(int rows, double **mat);
    
    double **read_matrix(int rows, int cols){
    
        double **mat = (double **) malloc(sizeof(double *)*rows);
        int i=0,j=0;
        for(i=0; i<rows; i++)
        /* Allocate array, store pointer  */
            mat[i] = (double *) malloc(sizeof(double)*cols); 
    
           for(i=0; i<rows; i++){
               for(j=0; j<cols; j++){
                   scanf("%lf",&mat[i][j]);
               }
           }
         return mat;
    }
    
    void print_matrix(int rows, int cols, double **mat){
        int i=0,j=0;
      for(i=0; i<rows; i++){    /* Iterate of each row */
            for(j=0; j<cols; j++){  /* In each row, go over each col element  */
                printf("%lf ",mat[i][j]); /* Print each row element */
            }
            printf("\n");
        }
    }
    
    void free_matrix(int rows, double **mat){
        int i=0;
        for(i=0;i<rows;i++)    
            free(mat[i]);
        free(mat);
    }
    
    int main(){
    
      double **matrix;
      int rows, cols;
      /* First matrix */
      printf("Matrix 1\n");
      printf("Enter # of rows and cols: ");
      scanf("%d%d",&rows,&cols);
      printf("Matrix, enter %d reals: \n",rows*cols);
      matrix = read_matrix(rows,cols); 
      printf("Your Matrix\n");  /* Print the entered data */
      print_matrix(rows,cols,matrix);
      free_matrix(rows, matrix);   /* Free the matrix */
    
      return 0;
     }
    

    Execution:

    :~$ gcc  exam.c
    :~$ ./a.out 
    Matrix 1
    Enter # of rows and cols: 3
    4
    Matrix, enter 12 reals: 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    9
    0
    1
    Your Matrix
    1.000000 2.000000 3.000000 4.000000 
    5.000000 6.000000 7.000000 8.000000 
    9.000000 9.000000 0.000000 1.000000 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a functions to modify strings in C. If I have
I was trying to write out some of the implementations of the string functions
I'm trying to write a function that shifts all the elements in an array
i am trying to write one function which return sql statement like function get_sql($name=0,$date_start=0,$date_end=0)
I'm trying to write some convenience functions in Scala for reading in arrays of
Trying to write a couple of functions that will encrypt or decrypt a file
I'm trying to write an INI file using the WritePrivateProfileString and WritePrivateProfileStruct functions. I
I am trying to implement a Client/Server model using TCpClient , with its Networkstream.Write()/Read()
OK so I have read a whole bunch of articles suggesting table-value functions and
I'm trying to create a function to read Morse code from one file, convert

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.