Classes/csci218/Labs/Lab9/lab9.c

149 lines
3.2 KiB
C

/* Name: Garrett Haldrup
lab9.c
Problem: Contains multiple functions that focus on nested loops and 2D arrays.
Certification of Authenticity:
I certify that this assignment is entirely my own work.
*/
#include <stdio.h>
void triangle();
void multiplicationTable();
void fractionalSequence(int n);
void twoDArrayMaxMin();
void triangleN(int n);
void multiplicationTableN(int n);
int main() {
triangle();
multiplicationTable();
fractionalSequence(15);
twoDArrayMaxMin();
triangleN(15);
multiplicationTableN(15);
return 0;
}
void triangle() {
int n = 10;
char symbol = '*';
printf("\nThis program will show a trangle with width of n = 10 and symbol '*'.\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j >= n - (i + 1)) {
printf("%c ", symbol);
} else {
printf(" ");
}
}
printf("\n");
}
}
void multiplicationTable() {
int n;
printf("\nThis program will take your input and make a n x n multiplication table.\n");
printf("Enter n for a n x n multiplcaiton table: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d\t", i * j);
}
printf("\n");
}
}
void fractionalSequence(int n) {
long long int product = 1;
printf("\nThis program will show the a(n) = n! from a(1) => a(%d)\n", n);
printf("{");
for (int i = 1; i <= n; i++) {
product *= i;
if (i == n) {
printf("%lld}\n", product);
} else {
printf("%lld, ", product);
}
}
}
void twoDArrayMaxMin() {
int NUM_ROWS, NUM_COLUMN;
int max, min, maxI, maxJ, minI, minJ;
printf("\nThis program will take your input for the size of a 2D array and values to fill the array then output the max and min of the 2D array\n");
printf("Enter number of rows: ");
scanf("%d", &NUM_ROWS);
printf("Enter number of columns: ");
scanf("%d", &NUM_COLUMN);
int nums[NUM_ROWS][NUM_COLUMN];
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLUMN; j++) {
printf("Enter value for nums[%d][%d]: ", i, j);
scanf("%d", &nums[i][j]);
if (!i && !j) {
max = nums[i][j];
maxI = i;
maxJ = j;
min = nums[i][j];
minI = i;
minJ = j;
} else if (nums[i][j] < min) {
min = nums[i][j];
minI = i;
minJ = j;
} else if (nums[i][j] > max) {
max = nums[i][j];
maxI = i;
maxJ = j;
}
}
}
printf("The max of nums is at nums[%d][%d] is: %d\n", maxI, maxJ, max);
printf("The min of nums is at nums[%d][%d] is: %d\n", minI, minJ, min);
}
void triangleN(int n) {
char symbol = '*';
int count = 1;
printf("\nThis program will show a trangle with width of n = %d and symbol '*'.\n", n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j >= n - count) {
printf("%c ", symbol);
} else {
printf(" ");
}
}
printf("\n");
count++;
}
}
void multiplicationTableN(int n) {
printf("\nThis program will make a %d x %d multiplication table.\n", n, n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
printf("%d\t", i * j);
}
printf("\n");
}
}