-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c71b8d1
commit 532f137
Showing
13 changed files
with
531 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
float perimeter(float a, float b, float c) | ||
{ | ||
return a + b + c; | ||
} | ||
|
||
float area(float a, float b, float c) | ||
{ | ||
float s = (a + b + c) / 2; | ||
return sqrt((s - a) * (s - b) * (s - c)); | ||
} | ||
|
||
int main() | ||
{ | ||
float a, b, c; | ||
char operation; | ||
|
||
printf("Enter first side: "); | ||
scanf("%f", &a); | ||
printf("Enter second side: "); | ||
scanf("%f", &b); | ||
printf("Enter third side: "); | ||
scanf("%f", &c); | ||
|
||
printf("Enter one of the followings: "); | ||
printf("\n(a) Perimeter of the triangle "); | ||
printf("\n(b) Area of the triangle "); | ||
printf("\n"); | ||
|
||
scanf(" %c", &operation); | ||
switch (operation) | ||
{ | ||
case 'a': | ||
printf("Result -> %f\n", perimeter(a, b, c)); | ||
break; | ||
case 'b': | ||
printf("Result -> %f\n", area(a, b, c)); | ||
break; | ||
default: | ||
printf("Invalid operation\n"); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <stdio.h> | ||
|
||
int find_largest(int *matrix, int rows, int cols) { | ||
int i, j, largest = *matrix; | ||
for (i = 0; i < rows; i++) { | ||
for (j = 0; j < cols; j++) { | ||
if (*(matrix + i * cols + j) > largest) { | ||
largest = *(matrix + i * cols + j); | ||
} | ||
} | ||
} | ||
return largest; | ||
} | ||
|
||
int main() { | ||
int m, n, i, j; | ||
printf("Enter number of rows and columns: "); | ||
scanf("%d %d", &m, &n); | ||
|
||
int matrix[m][n]; | ||
printf("Enter matrix elements: \n"); | ||
for (i = 0; i < m; i++) { | ||
for (j = 0; j < n; j++) { | ||
scanf("%d", &matrix[i][j]); | ||
} | ||
} | ||
|
||
int largest = find_largest(&matrix[0][0], m, n); | ||
printf("Largest element in the matrix is %d\n", largest); | ||
|
||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <stdio.h> | ||
|
||
void multiply_matrices(int *matrix_one, int *matrix_two, int m, int n) | ||
{ | ||
int i, j, k; | ||
int result[m][m]; | ||
|
||
for (i = 0; i < m; i++) | ||
{ | ||
for (j = 0; j < m; j++) | ||
{ | ||
|
||
result[i][j] = 0; | ||
for (k = 0; k < n; k++) | ||
{ | ||
result[i][j] += *(matrix_one + i * n + k) * *(matrix_two + k * m + j); | ||
} | ||
} | ||
} | ||
|
||
printf("Resultant matrix: \n"); | ||
for (i = 0; i < m; i++) | ||
{ | ||
printf("["); | ||
for (j = 0; j < m; j++) | ||
{ | ||
printf(" %d ", result[i][j]); | ||
} | ||
printf("]\n"); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int m, n; | ||
printf("Enter m and n (mxn) (nxm): "); | ||
scanf("%d %d", &m, &n); | ||
|
||
int matrix_one[m][n]; | ||
int matrix_two[n][m]; | ||
int i, j; | ||
|
||
printf("Enter matrix one elements: \n"); | ||
for (i = 0; i < m; i++) | ||
{ | ||
for (j = 0; j < n; j++) | ||
{ | ||
printf("Enter element at (%d, %d): ", i, j); | ||
scanf("%d", &matrix_one[i][j]); | ||
} | ||
} | ||
|
||
printf("Enter matrix two elements: \n"); | ||
for (i = 0; i < n; i++) | ||
{ | ||
for (j = 0; j < m; j++) | ||
{ | ||
printf("Enter element at (%d, %d): ", i, j); | ||
scanf("%d", &matrix_two[i][j]); | ||
} | ||
} | ||
|
||
multiply_matrices((int *)matrix_one, (int *)matrix_two, m, n); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdio.h> | ||
|
||
void input_matrix(int *matrix, int rows, int cols) | ||
{ | ||
int i, j; | ||
printf("Enter matrix elements: \n"); | ||
for (i = 0; i < rows; i++) | ||
{ | ||
for (j = 0; j < cols; j++) | ||
scanf("%d", &matrix[i * cols + j]); | ||
} | ||
} | ||
|
||
void print_matrix(int *matrix, int rows, int cols) | ||
{ | ||
int i, j; | ||
printf("Matrix: \n"); | ||
for (i = 0; i < rows; i++) | ||
{ | ||
printf("["); | ||
for (j = 0; j < cols; j++) | ||
printf(" %d ", matrix[i * cols + j]); | ||
printf("]\n"); | ||
} | ||
} | ||
|
||
void print_row_averages(int *matrix, int rows, int cols) | ||
{ | ||
int i, j; | ||
printf("Row averages: \n"); | ||
for (i = 0; i < rows; i++) | ||
{ | ||
int sum = 0; | ||
for (j = 0; j < cols; j++) | ||
sum += matrix[i * cols + j]; | ||
printf("%d\n", sum / cols); | ||
} | ||
} | ||
|
||
void print_col_averages(int *matrix, int rows, int cols) | ||
{ | ||
int i, j; | ||
printf("Column averages: \n"); | ||
for (i = 0; i < cols; i++) | ||
{ | ||
int sum = 0; | ||
for (j = 0; j < rows; j++) | ||
sum += matrix[j * cols + i]; | ||
printf("%d\n", sum / rows); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
int m, n; | ||
printf("Enter number of rows and columns: "); | ||
scanf("%d %d", &m, &n); | ||
|
||
int matrix[m][n]; | ||
input_matrix(&matrix[0][0], m, n); | ||
|
||
print_matrix(&matrix[0][0], m, n); | ||
print_row_averages(&matrix[0][0], m, n); | ||
print_col_averages(&matrix[0][0], m, n); | ||
|
||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Develop a top-down modular program that will perform the following tasks: | ||
// ``` | ||
// (a) Read two integer arrays with unsorted elements. | ||
// (b) Sort them in ascending order | ||
// (c) Merge the sorted arrays | ||
// (d) Print the sorted list | ||
// ``` | ||
// Use functions for carrying out each of the above tasks. The main function should have only function calls. | ||
|
||
#include <stdio.h> | ||
|
||
void input_array(int *array, int size) | ||
{ | ||
int i; | ||
printf("Enter array elements: \n"); | ||
for (i = 0; i < size; i++) | ||
scanf("%d", &array[i]); | ||
} | ||
|
||
void print_array(int *array, int size) | ||
{ | ||
int i; | ||
printf("Array: \n"); | ||
for (i = 0; i < size; i++) | ||
printf("%d ", array[i]); | ||
printf("\n"); | ||
} | ||
|
||
void sort_array(int *array, int size) | ||
{ | ||
int i, j; | ||
for (i = 0; i < size; i++) | ||
{ | ||
int min = array[i], min_index = i; | ||
for (j = i + 1; j < size; j++) | ||
{ | ||
if (array[j] < min) | ||
{ | ||
min = array[j]; | ||
min_index = j; | ||
} | ||
} | ||
int temp = array[i]; | ||
array[i] = array[min_index]; | ||
array[min_index] = temp; | ||
} | ||
} | ||
|
||
void merge_arrays(int *array_one, int *array_two, int *result, int size_one, int size_two) | ||
{ | ||
int i, j; | ||
for (i = 0; i < size_one; i++) | ||
result[i] = array_one[i]; | ||
for (j = 0; j < size_two; j++) | ||
result[i + j] = array_two[j]; | ||
} | ||
|
||
void operations(void) | ||
{ | ||
int m, n; | ||
printf("Enter size of array one and array two: "); | ||
scanf("%d %d", &m, &n); | ||
int array_one[m], array_two[n]; | ||
input_array(array_one, m); | ||
input_array(array_two, n); | ||
|
||
sort_array(array_one, m); | ||
sort_array(array_two, n); | ||
|
||
int result[m + n]; | ||
merge_arrays(array_one, array_two, result, m, n); | ||
sort_array(result, m+n); | ||
|
||
print_array(result, m + n); | ||
} | ||
|
||
int main() | ||
{ | ||
operations(); | ||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <stdio.h> | ||
|
||
void copy_string(char *one, char *two) | ||
{ | ||
int i; | ||
for (i=0; one[i] != '\0'; i++) | ||
two[i] = one[i]; | ||
two[i] = '\0'; | ||
return; | ||
} | ||
|
||
void compare_string(char *one, char *two) | ||
{ | ||
int i; | ||
for (i=0; one[i] != '\0'; i++) | ||
if (one[i] != two[i]) | ||
break; | ||
if (one[i] == '\0' && two[i] == '\0') | ||
printf("Strings are equal\n"); | ||
else | ||
printf("Strings are not equal\n"); | ||
return; | ||
} | ||
|
||
void concat_string(char *one, char *two) | ||
{ | ||
int i, j; | ||
for (i=0; one[i] != '\0'; i++); | ||
for (j=0; two[j] != '\0'; j++) | ||
one[i+j] = two[j]; | ||
one[i+j] = '\0'; | ||
return; | ||
} | ||
|
||
int main() | ||
{ | ||
char string_one[100], string_two[200]; | ||
printf("Enter your string: "); | ||
fgets(string_one, 100, stdin); | ||
|
||
copy_string(string_one, string_two); | ||
printf("Your second string is: %s", string_two); | ||
|
||
compare_string(string_one, string_two); | ||
|
||
concat_string(string_one, string_two); | ||
printf("Your concatenated string is: %s", string_one); | ||
|
||
return 0; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <stdio.h> | ||
|
||
int char_search_inside_string(char *string, char c) | ||
{ | ||
int i; | ||
for (i=0; string[i] != '\0'; i++) | ||
if (string[i] == c) | ||
return i; | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
char string[100], c; | ||
printf("Enter your string: "); | ||
fgets(string, 100, stdin); | ||
printf("Enter your character: "); | ||
scanf("%c", &c); | ||
|
||
int index = char_search_inside_string(string, c); | ||
if (index == -1) | ||
printf("Character not found\n"); | ||
else | ||
printf("Character found at index %d\n", index); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.