Commit 197c5f10 by unknown

changes and commented on code

parent d8916e69
......@@ -3,26 +3,34 @@
* Author: Tran Minh Huy Vu
*
* Created on October 4, 2018, 1:50 PM
* Description: Function takes in 3 values, initial, increment and limit to display the
* y value of a defined function
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// This function prints the top of the table
void createTable() {
printf("╒═══╤═══════════════╤═══════════════╕\n");
printf("│ nr│ x │ y │\n");
printf("╞═══╪═══════════════╪═══════════════╡\n");
}
// Number is incremented every time addToTable is called
int number = 1;
// addToTable adds a row to the table every time it is called, it have 2 arguments,
// one is the x value and one is the y.
void addToTable(float a, float b) {
// If b is not a number, display NaN
if (isnan(b)) {
if (a < 10) {
printf("│ %d │ %f │ NaN │\n", number, a);
} else if (a < 100) {
printf("│ %d │ %f │ NaN │\n", number, a);
}
// if number is < 10, we will assign more spaces to the table
} else if (number < 10) {
if (a < 10) {
if (b < 10) printf("│ %d │ %f │ %f │\n", number, a, b);
......@@ -31,6 +39,7 @@ void addToTable(float a, float b) {
if (b < 10) printf("│ %d │ %f │ %f │\n", number, a, b);
else if (b < 100) printf("│ %d │ %f │ %f │\n", number, a, b);
}
// now we remove more spaces
} else if (number < 100) {
if (a < 10) {
if (b < 10) printf("│%d │ %f │ %f │\n", number, a, b);
......@@ -43,17 +52,28 @@ void addToTable(float a, float b) {
number++;
}
// This function prints the end of the table. It is called last
void endTable() {
printf("╘═══╧═══════════════╧═══════════════╛");
}
int main() {
/*
Defining variables:
* x is initial x value, which is user defined
* y is the y value, which we will change later
* lim is the limit of x, which is user defined
* incre is the increment of x through every interval, which is user defined
*/
float x, y, lim, incre;
float yResults[15];
printf("Please input starting value: ");
scanf("%f", &x);
printf("Please input increment value: ");
scanf("%f", &incre);
// If increment is snaller than or equal to 0, we return an error
if (incre <= 0) {
printf("Increment have to be > 0");
return 1;
......@@ -61,20 +81,32 @@ int main() {
printf("Please input limit value: ");
scanf("%f", &lim);
// If limit is smaller than or equal to the starting value (x), we also return an error
if (lim <= x) {
printf("Limit have to be bigger than starting value");
return 1;
}
printf("Result for: ln(x + 5) / sqrt(7 + 5x + x^2)\n\n");
// Function createTable() is called to initialize the header of table
createTable();
int tooMuch = 0;
for (float i = 0; i < lim && !tooMuch; i = i + incre) {
// As i = 0; i < lim && !tooMuch; i += incre, i increases by incre every time
// and the function loop until i > lim or tooMuch is true
for (float i = 0; i < lim && !tooMuch; i += incre) {
y = log(i + 5) / sqrt(7 + 5*i + i*i);
addToTable(i, y);
// This stores and display the result (y) by using the addToTable function
yResults[i] = y;
addToTable(i, yResults[i]);
if (isnan(y)) y = lim - 1;
// As number is = 16, it is too much and we stop the loop
if (number >= 16) tooMuch = 1;
}
// function endTable is called to end the table
endTable();
return (EXIT_SUCCESS);
}
......
/*
* File: homework1.c
* Author: Tran Minh Huy Vu
*
* Created on October 4, 2018, 1:50 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void createTable() {
printf("╒═══╤═══════════════╤═══════════════╕\n");
printf("│ nr│ x │ y │\n");
printf("╞═══╪═══════════════╪═══════════════╡\n");
}
int number = 1;
void addToTable(float a, float b) {
if (isnan(b)) {
if (a < 10) {
printf("│ %d │ %f │ NaN │\n", number, a);
} else if (a < 100) {
printf("│ %d │ %f │ NaN │\n", number, a);
}
} else if (number < 10) {
if (a < 10) {
if (b < 10) printf("│ %d │ %f │ %f │\n", number, a, b);
else if (b < 100) printf("│ %d │ %f │ %f │\n", number, a, b);
} else if (a < 100) {
if (b < 10) printf("│ %d │ %f │ %f │\n", number, a, b);
else if (b < 100) printf("│ %d │ %f │ %f │\n", number, a, b);
}
} else if (number < 100) {
if (a < 10) {
if (b < 10) printf("│%d │ %f │ %f │\n", number, a, b);
else if (b < 100) printf("│ %d│ %f │ %f │\n", number, a, b);
} else if (a < 100) {
if (b < 10) printf("│%d │ %f │ %f │\n", number, a, b);
else if (b < 100) printf("│%d │ %f │ %f │\n", number, a, b);
}
}
number++;
}
void endTable() {
printf("╘═══╧═══════════════╧═══════════════╛");
}
int main() {
float x, y, lim, incre;
printf("Please input starting value: ");
scanf("%f", &x);
printf("Please input increment value: ");
scanf("%f", &incre);
if (incre <= 0) {
printf("Increment have to be > 0");
return 1;
}
printf("Please input limit value: ");
scanf("%f", &lim);
if (lim <= x) {
printf("Limit have to be bigger than starting value");
return 1;
}
printf("Result for: ln(x + 5) / sqrt(7 + 5x + x^2)\n\n");
createTable();
int tooMuch = 0;
for (float i = 0; i < lim && !tooMuch; i = i + incre) {
y = log(i + 5) / sqrt(7 + 5*i + i*i);
addToTable(i, y);
if (isnan(y)) y = lim - 1;
if (number >= 16) tooMuch = 1;
}
endTable();
return (EXIT_SUCCESS);
}
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