기록하고 까먹지 말기

과제9 - Pointer Dynamic (6 ~ 9) 본문

전공/C

과제9 - Pointer Dynamic (6 ~ 9)

yha97 2021. 6. 22. 14:38

#include <stdio.h>
#include <stdlib.h>

int main(){
	int n, i= 0; // declaring variables
	int smallest ; // smallest float
	printf("Enter the total number of elements: ");
	scanf("%d", &n); // enter size of inputs
	
	int* ptr = (int*)malloc(n*sizeof(int)); // declaring array with memory allocation
	
	for(i=0; i<n; i++){
		scanf("%d", (ptr+i)); // entering numbers
		// finding smallest number in entered values
		if(i == 0)
			smallest = *(ptr+i);
		if(smallest > *(ptr+i))
			smallest = *(ptr+i);
	}
	// printing output
	printf("Smallest element is %d", smallest);
	
	free(ptr); // erasing allocatied memory
	
	return 0;
}

 

 

 

#include <stdio.h>

int main(){
	// decaring variables
	int n, i, j, k = 0;
	printf("Enter number of rows : ");
	scanf("%d", &n); // inputs total rows
	int arr[100][100] = {0, }; // initializing array
	
	arr[0][0] = 1; // first row's value
	
	// setting Paskal's triangles
	for(i=1; i<n; i++){
		for(j=0; j<=i; j++){
			if(j == 0 || j == i){
				arr[i][j] = 1; // the value in first or last column of row is 1
				continue;
			}
			arr[i][j] = arr[i-1][j-1] + arr[i-1][j]; // setting value under Pascal's rule
		}
	}
	
	// printing output
	for(i=0; i<n; i++){
		for(k=1; k<(n-i); k++)
			printf(" "); // space before printing values of elements
		for(j=0; j<=i; j++){
			printf("%d ", arr[i][j]); // printing elements
		}
		printf("\n"); // going to next line
	}
	
	return 0;
}

참고자료 : https://ko.wikipedia.org/wiki/%ED%8C%8C%EC%8A%A4%EC%B9%BC%EC%9D%98_%EC%82%BC%EA%B0%81%ED%98%95

 

 

 

#include <stdio.h>

int main(){
	// initializing variables and array A and B
	int a[3][3] = {0, };
	int b[3][3] = {0, };
	int i, j = 0;
	
	// getting input elements from user
	printf("Enter elements in matrix A of size 3x3:\n");
	for(i=0; i<3; i++){
		for(j=0; j<3; j++)	
			scanf("%d", &a[i][j]);
	}
	printf("\nEnter elements in matrix B of size 3x3:\n");
	for(i=0; i<3; i++){
		for(j=0; j<3; j++)	
			scanf("%d", &b[i][j]);
	}
	
	// printing the output based on sample below with calculating sum between two metrices
	printf("\nSum of matrices A+B = \n");
	for(i=0; i<3; i++){
		for(j=0; j<3; j++){
			printf("%d ", a[i][j]+b[i][j]); // printing with calculating
		}
		printf("\n"); // goint new lines
	}
	return 0;
}

 

 

 

#include <stdio.h>

int main(){
	// initializing variables and array A and B
	int a[3][3] = {0, };
	int b[3][3] = {0, };
	int i, j = 0;
	
	// getting input elements from user
	printf("Enter elements in matrix A of size 3x3:\n");
	for(i=0; i<3; i++){
		for(j=0; j<3; j++)	
			scanf("%d", &a[i][j]); // entering Matrix A
	}
	printf("\nEnter elements in matrix B of size 3x3:\n");
	for(i=0; i<3; i++){
		for(j=0; j<3; j++)	
			scanf("%d", &b[i][j]); // Entering Matrix B
	}
	
	for(i=0; i<3; i++){
		for(j=0; j<3; j++){
			if(a[i][j] != b[i][j]){ // there is uncoindicence between a[i][j] and b[i][j]
				printf("\nMatrix A is not equal to Matrix B"); // printing message that they are not same 
				return 0; // then finish program
			}
		}
	}
	printf("\nMatrix A is equal to Matrix B"); // all coincidence -> printing message that they are same
	return 0;
}

 

'전공 > C' 카테고리의 다른 글

과제11 - final (1)  (0) 2021.06.22
과제10 - StructUnion (1 ~ 7)  (0) 2021.06.22
과제9 - Pointer Dynamic (1 ~ 5)  (0) 2021.06.22
과제8 - Pointer Array (6 ~ 10)  (0) 2021.06.22
과제8 - Pointer_Array (1 ~ 5)  (0) 2021.06.22