Last active
March 27, 2019 20:31
-
-
Save JaeDukSeo/3f120400eaf059a74300981a74d95650 to your computer and use it in GitHub Desktop.
c solution for CPS 125 students
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
// this is the lab 7 homework problem tenfold (this is not the solution rather reference code) | |
// see here http://cps125.scs.ryerson.ca/labs/manual.html | |
void tenfold(int* array_pointer1,int* array_pointer2,int array_size){ | |
for (int i=0;i<array_size;i++){ | |
if(array_pointer1[i]<0){ | |
array_pointer2[i] = array_pointer1[i] + 4; | |
}else{ | |
array_pointer2[i] = array_pointer1[i] ; | |
} | |
} | |
} | |
// check if the diag element of the matrix is the same | |
// This is one of the question from lab 8 | |
int checkdiag(int matrix[][3],int size){ | |
int first_number = matrix[0][0]; | |
for(int i =0;i<size;i++){ | |
if (matrix[i][i]!=first_number){ | |
return 0; | |
} | |
} | |
return 1; | |
} | |
// return the sum of the diag elements | |
// This is one of the solution for the final w 2009 | |
int sumdiag(int size, int matrix[][size]){ | |
int sum_number = 0; | |
for(int i=0;i<size;i++){ | |
sum_number = sum_number + matrix[i][i]; | |
} | |
return sum_number; | |
} | |
// check if adjunct element of the array is the same | |
// This is one of the solution for the final w 2009 | |
void checkarray(int array_compare[],int length){ | |
for (int i=0;i<(length-1);i++){ | |
if(array_compare[i]==array_compare[i+1]){ | |
printf("Cells %d and %d contains %d \n",i,i+1,array_compare[i]); | |
} | |
} | |
} | |
int main(void) { | |
// We are going to read from a txt file that contains | |
// data that looks like this | |
// 3 | |
// 1 2 3 | |
// 4 5 6 | |
// 7 8 9 | |
// | |
// so the first number shows how many element that are | |
// in a square matrix and starting from the next row the matrix itself | |
int how_many_rows; | |
FILE* fp; | |
fp = fopen("one.txt","r"); | |
fscanf(fp,"%d",&how_many_rows); | |
printf("%d\n",how_many_rows); | |
// reading the matrix to the 2D array | |
int matrix[how_many_rows][how_many_rows]; | |
for (int i=0;i<how_many_rows;i++){ | |
for (int j=0;j<how_many_rows;j++){ | |
fscanf(fp,"%d",&matrix[i][j]); | |
} | |
} | |
printf("-------------------------\n"); | |
// Printing out the value of the 2D matrix | |
for (int i=0;i<how_many_rows;i++){ | |
for (int j=0;j<how_many_rows;j++){ | |
printf("%d ",matrix[i][j]); | |
} | |
printf("\n"); | |
} | |
// Are the diag element the same? | |
printf("-------------------------\n"); | |
printf("CHECK DIAG\n"); | |
printf("%d",checkdiag(matrix,how_many_rows)); | |
printf("\nSUM DIAG\n"); | |
printf("%d",sumdiag(how_many_rows,matrix)); | |
printf("\n-------------------------\n"); | |
// This is one of the solution for the final w 2009 | |
//THIS IS A STRING NOT A CHAR | |
char operation[7]; | |
int number1,number2; | |
scanf("%s%d%d", operation,&number1,&number2); | |
printf("%s",operation); | |
// string comparison - if average or not | |
if(strcmp(operation,"average")==0){ | |
printf("\nCalculate average %lf",(number1+number2)/2.0); | |
}else{ | |
printf("\nCalculate average %lf",(number1+number2)/1.0); | |
} | |
printf("\n---------------------\n"); | |
// this is also one of the question in the final | |
int afdhakfhdka[] = {34,66,77,99,55,55,44,44,44,22}; | |
int length_of_arry =(int) sizeof(afdhakfhdka) / sizeof(afdhakfhdka[0]); | |
checkarray(afdhakfhdka,length_of_arry); | |
int array1[9] = {3,4,5,6,7, -8,-9,1,2}; | |
int array2[9]; | |
tenfold(array1,array2,9); | |
for (int i=0;i<9;i++){ | |
printf("%d ",array1[i]); | |
} | |
printf("\n-------------\n"); | |
for (int i=0;i<9;i++){ | |
printf("%d ",array2[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment