Question

Using C programming

  1. 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.

  2. 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.

  3. 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.

  4. Check your program with the same data files as before.


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

#include <bits/stdc++.h>

#include <fstream>


using namespace std;


int checkdiag2(vector< vector<int> > &matrix, int size) {

  int count=0, repeated_number;

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

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

      // antidiagonal condition

      if ( (i + j + 1) == size ) {

        if(i==0) {

          repeated_number = matrix[i][j];

          count++;

        }

        else

          if(matrix[i][j]==repeated_number)

          count++;

        else

          break;

      }

    }

  }

  if(count==size)

    return 1;

  else

    return 0;

}


int main()

{

  int size;

  ifstream MyReadFile("filename.txt");

  while (getline (MyReadFile, myText)) {

    size = stoi(myText);

  }

  

  // Dynamic 2D array

  // I have itialized the 2D array, because there is no instruction about it

  vector< vector<int> > matrix( size , vector<int> (size, 0));

  

  // You can also input the 2D array from the user like this :

  // vector< vector<int> > matrix( size , vector<int> (size));

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

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

  // // antidiagonal condition

  // cin>>matrix[i][j];

  // }

  // }

  

  int value = checkdiag2(matrix,size);

  if(value)

    cout<<"The matrix is "<<size<<"x"<<size<<" and all the numbers on the antidiagonal are the same.";

  else

    cout<<"The matrix is "<<size<<"x"<<size<<" and all the numbers on the antidiagonal are not same.";

}



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
  • Write another function called checkdiag2 (int checkdiag2 (int matrix[][100], int size)) that will return 1 if...

    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...

  • Using C programming...

    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.The function is called checkdiag (int checkdiag (int matrix[][100], int...

  • Read your notes concerning 2D arrays with particular attention to the syntax of passing arrays as...

    Read your notes concerning 2D arrays with particular attention to the syntax of passing arrays as parameters to functions. Write the pseudocode (algorithm) for the following program definition: 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...

  • 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...

  • Write a function called arrFactory that takes the address of an int** and two int values,...

    Write a function called arrFactory that takes the address of an int** and two int values, rows and cols. The function will use dynamic memory allocation such that after the function call the int** can be used as a 2D array with the specified number of rows and columns. All elements of the array must be set to 0 within the function without iterating through each element after the array is created. The function has a return type of void....

  • 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...

  • Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a...

    Write a class named RowSort which has the following members. Private: (1) double matrix 0O (a 2D array): (2) int columns; (3) int rows Public: (1) A default constructor that creates a 2D array, 8 6 4 (2) A constructor that takes three values for the three private members, and creates a 2D array accordingly. (3) The "get" and "set" methods for the three private members. (4) A method that displays a 2D array, stored in the private member matrix,...

  • C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if...

    C++ no std: cout but cout<<" ". also nothing ".h": in the header files: Also if statements should be used for the file portion. (filein.eof is not aloud) generate integer random numbers and place them in a file. Each number on a single line. The numbers should range between 1-200 and the program should generate between 100-150 numbers. This means each time the program is executed, a file is created containing between 100-150 numbers with values between 1-200. Make sure...

  • Create a class called Lab7b and in it implement all of the methods below. Also, write...

    Create a class called Lab7b and in it implement all of the methods below. Also, write a main method with test calls to all of these methods. Don’t forget to turn in your file to Canvas before the end of the lab today. int[][] random(int N, int start, int end) returns an N-by-N matrix of random integers ranging from start to end; int rowSum(int[][] a, int i) returns the sum of the elements in row i of the 2-D array...

  • 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...

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