Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void condition(){
- printf("A real square matrix of order 2n is given. The numbers denote submatrices of order n\n");
- printf(" -------\n");
- printf("| 1 | 2 |\n");
- printf(" ------- \n");
- printf("| 3 | 4 |\n");
- printf(" ------- \n");
- printf("Get a new matrix:\n");
- printf(" -------\n");
- printf("| 4 | 3 |\n");
- printf(" ------- \n");
- printf("| 1 | 2 |\n");
- printf(" -------\n");
- }
- void outputMatrix(int** mass, int size){
- for (int i = 0; i < size; i++){
- for (int j = 0; j < size; j++) {
- printf( "%d \t", mass[i][j]);
- }
- printf("\n");
- }
- }
- void swap(int** mass,int size){
- int temp;
- int center;
- center = size / 2;
- for (int i = 0; i < (size - center); i++) {
- for (int j = 0; j < (size - center); j++) {
- temp = mass[j][i];
- mass[j][i] = mass[j + center][i + center];
- mass[j + center][i + center] = temp;
- }
- }
- for (int i = size - 1; i >= center ; i--) {
- for (int j = 0; j < (center); j++) {
- temp = mass[j][i];
- mass[j][i] = mass[j + center][i - center];
- mass[j + center][i - center] = temp;
- }
- }
- printf("The resulting matrix: \n");
- outputMatrix(mass,size);
- }
- void outputInFile(int** mass,int size){
- char nameOfFile[20];
- puts("Enter the name of the file you want to display data in:\n");
- scanf("%s",nameOfFile);
- strcat(nameOfFile,".txt");
- FILE* out = fopen(nameOfFile, "w+");
- fprintf(out,"%s \n","The resulting matrix: ");
- for(int i = 0; i < size; i++ ){
- for(int j = 0; j < size; j++ ){
- fprintf(out,"%d \t",mass[i][j]);
- }
- fprintf(out,"\n");
- }
- printf("Matrix successfully written to file %s",&nameOfFile);
- fclose(out);
- }
- int main() {
- int size;
- char nameOfFile[20];
- condition();
- printf("Enter the name of the file from which you want to read the information: \n");
- scanf("%s",nameOfFile);
- strcat(nameOfFile,".txt");
- FILE* in = fopen(nameOfFile, "r+");
- fscanf(in,"%d", &size);
- if (size % 2 == 0) {
- int **mass = (int **) malloc(size * sizeof(int *));
- for (int i = 0; i < size; i++) {
- mass[i] = (int *) malloc(size * sizeof(int));
- for (int j = 0; j < size; j++) {
- fscanf(in, "%d", &mass[i][j]);
- }
- }
- fclose(in);
- printf("The original matrix of order 2n:\n");
- outputMatrix(mass, size);
- swap(mass, size);
- outputInFile(mass, size);
- }else{
- printf("Mistake! Modify the file, it should only contain numbers,\n"
- " and the size of the matrix should be 2n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement