Question

Using C programming...

  1. Write a text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.


  2. The function is called checkdiag (int checkdiag (int matrix[][100], int size)) that will return 1 if all the numbers on the main diagonal are the same and 0 otherwise.


  3. Your program will read the array from the file, check the diagonal and print a report containing the size and the status of the diagonal. A typical report would look like The matrix is 3x3 and all the numbers on the main diagonal are the same.




0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

int checkdiag (int matrix[][100], int size);

int main(){

   int matrix[100][100];

   int size;

   char filename[30];

   FILE *fp;

   int i,j;

   

   printf("Enter input filename: ");

   scanf("%s", filename);

   

   fp = fopen(filename, "r");

   if(fp == NULL){

      printf("ERROR: could not open file %s\n", filename);

      return 1;

   }

   

   fscanf(fp, "%d", &size);

   for(i = 0; i < size; i++){

      for(j = 0; j < size; j++){

         fscanf(fp, "%d", &matrix[i][j]);

      }

   }

   

   if(checkdiag(matrix, size))

      printf("The matrix is %dx%d and all the numbers on the main diagonal are the same.\n", size, size);

   else

      printf("The matrix is %dx%d and all the numbers on the main diagonal are NOT the same.\n", size, size);

   return 0;

}


int checkdiag (int matrix[][100], int size){

   int i;

   for(i = 1; i < size; i++){

      if(matrix[i][i] != matrix[i-1][i-1])

         return 0;

   }

   return 1;

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Using C programming...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C programming

    Write another function called checkdiag2 (int checkdiag2 (int matrix[][100], int size)) that will return 1 if all the numbers on the antidiagonal are the same and 0 otherwise.Rewrite your main program to use 2D dynamic allocation instead for your array. Your array will have rows and columns depending of the first integer read from the file.Call your function from the main program. A typical report would look like The matrix is 8x8 and all the numbers on the antidiagonal are the same.Check your...

  • Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the...

    Diagonal Difference HackerRank Pseudocode and C++: Given a square matrix, calculate the absolute difference between the sums of its diagonals. Function Description Complete the diagonalDifference function described below to calculate the absolute difference between diagonal sums. diagonalDifference( integer: a_size_rows, integer: a_size_cols, integer array: arr) Parameters: a_size_rows: number of rows in array a_size_cols: number of columns in array a: array of integers to process Returns: integer value that was calculated Constraints -100 < = elements of the matrix < = 100...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • In c++ programming, can you please edit this program to meet these requirements: The program that...

    In c++ programming, can you please edit this program to meet these requirements: The program that needs editing: #include <iostream> #include <fstream> #include <iomanip> using namespace std; int cal_avg(char* filenumbers, int n){ int a = 0; int number[100]; ifstream filein(filenumbers); if (!filein) { cout << "Please enter a a correct file to read in the numbers from"; }    while (!filein.eof()) { filein >> number[a]; a++; } int total = number[0]; for (a = 0; a < n; a++) {...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • 18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one...

    18.1 Lab Lesson 11 (Part 1 of 1) Part of lab lesson 11 There in one part to lab lesson 11. The entire lab will be worth 100 points. Lab lesson 11 part 1 is worth 100 points For part 1 you will have 80 points if you enter the program and successfully run the program tests. An additional 20 points will be based on the style and formatting of your C++ code. Style points The 20 points for coding...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT