Commit 7fa2994f by tranvu

adding lab 3 work

parent 5f78b4ba
Showing with 119 additions and 0 deletions
#include <stdio.h>
#include <stdlib.h>
void task1 ();
void task2 ();
void task3 ();
int main() {
printf("Please input the task you want to check: \n1: Task 1\n2: Task 2\n3: Task 3\n");
int input = 0;
scanf("%d", &input);
switch (input) {
case 1:
task1();
break;
case 2:
task2();
break;
case 3:
task3();
break;
default:
printf("Wtf did you just input?!?!?");
}
return (EXIT_SUCCESS);
}
void printInt(int a[], int size) {
printf("All even numbers:\n");
for (int i = 0; i < size; i++) {
if (a[i] % 2 == 0) {
printf("%d\n", a[i]);
}
}
printf("All odd numbers:\n");
for (int i = 0; i < size; i++) {
if (a[i] % 2) {
printf("%d\n", a[i]);
}
}
printf("Reverse crap:\n");
for (int i = size - 1; i >= 0; i--) {
printf("%d\n", a[i]);
}
}
void task1 () {
int size = 10;
int a[] = { 1,2,3,4,5,6,7,8,9,10 };
printInt(a, size);
}
void task2 () {
int length;
printf("Please input the length of the array: ");
scanf("%d", &length);
int a[length];
int x = 0;
int y = 1;
int z;
for (int i = 0; i < length; i++) {
if (i < 2) a[i] = i;
else {
z = x + y;
x = y;
y = z;
a[i] = z;
}
}
printf("Printing %d Fibonacci numbers:\n", length);
for (int i = 0; i < length; i++) {
printf("%d\n", a[i]);
}
printInt(a, length);
}
void task3 () {
int length;
printf("Please input array length: ");
scanf("%d", &length);
printf("Please input %d numbers: \n", length);
int a[length];
for (int i = 0; i < length; i++) {
scanf("%d", &a[i]);
}
int prev = a[0];
int isTrue = 1;
// Ascending check
for (int i = 1; i < length; i++) {
if (prev >= a[i]) {
isTrue = 0;
}
}
if (isTrue) {
printf("Integers is ascending!");
return;
}
// Descending check
prev = a[0];
isTrue = 1;
for (int i = 1; i < length; i++) {
if (prev <= a[i]) isTrue = 0;
}
if (isTrue) {
printf("Integers is descending!");
return;
}
// No match
printf("It is neither ascending or descending!");
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment