You are on page 1of 5

C2 lab logbook: Flows and conditional loop

By Mr Nicholas Ng Zhi Cheng


Student Id: 32420277
Section 3 Part 1
1.1
I have created an endless loop which can make an increment of 1 to infinity to the variable x.

#include<stdio.h>

int main()

unsigned long int x;

for(x=0;x=x+1;x=x++)

printf("%d\n", x);

return(0);

1.2
The function for math is included and PI is defined. The value of y as sine is calculated and the
formula is written into the code. The frequency of the sine function is added as FREQ to control its
frequency. The value of y is adjusted by assigning float y = 0 and while = (1) so that it only falls within
a range of 0 to 1.

#include<stdio.h>

#include<math.h>

#define PI 3.142

int main()

unsigned long int x = 0;

float y = 0;

int FREQ = 1;

while=(1)

y=(sin(x*PI/180)+1)/2;

printf("%d\n", y);
return(0);

Section 3 Part 2
I have declared and define the function plotval() where a floating point and int is taken as an
argument. A sine wave graph is plotted as I compile and run the code.
#include<stdio.h>
#include<math.h>
#define PI 3.14

void plotval(float point, int plot_width)


{
int position = plot_width*point;
int i =0;
for(i=0; i<position ; i++)
{
printf(" ");
}
printf("*\n");
}

int main()
{
unsigned long int x = 0;
float y = 0;
int FREQ = 1;
int size = 55;
while(1)
{
x = x + 1;
printf("x=%6d", x);
y= (sin(FREQ*x*PI/180)+1)/2;
printf("y=%.2f", y);
plotval(y,size);
}

return 0;
}

Section 3 Part 3
To create tick mark in which that it follows the numbers at regular intervals, the if else
statement is used. By using the modulo operator “%”, equivalent number of spaces will be
printed. A variable is assigned as an integer declared as tick.
#include<stdio.h>
#include<math.h>
#define PI 3.14

void plotval(float point, int plot_width)


{
int position = plot_width*point;
int i =0;
for(i=0; i<position ; i++)
{
printf(" ");
}
printf("*\n");
}
int main()
{
unsigned long int x = 0;
float y = 0;
int FREQ = 1;
int size = 55;
int tick = 10;
x = 0;
while(1)
{
x = x + 1;
printf("x=%6d", x);
y= (sin(FREQ*x*PI/180)+1)/2;
printf("y=%.2f", y);
plotval(y,size);
}

if (x%tick==10)
{
printf("-");
}
else
{
printf(" ");
}

return 0;
}
In writing this logbook, I have referred to several classmates for solutions. I have asked Lee
Mun Hooi, Irfan, Kylie Tan and Lai Jia Jin for help. I get to understand more as I proceed with
working on the codes. I have also listened to Dr Ivan Ling about how and what to start on
writing the codes as I am stuck on many areas. I have also accessed a few websites to search
for answers regarding my problems.

Reference:
1) https://overiq.com/c-programming-101/the-infinite-loop-in-c/
2) https://www.tutorialspoint.com/c_standard_library/math_h.htm
3) https://stackoverflow.com/questions/45402229/draw-a-sine-wave-using-c

You might also like