Skip to content

add a new file to repo #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions practical102/Conversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <math.h>

int main(void) {

/* Declare variables */
int i,inum,tmp,numdigits;
float fnum;
char binnum[60];

/* Intialise 4-byte integer */
inum = 33554431;
/* Convert to 4-byte float */
fnum = (float) inum;


/* Convert to binary number (string)*/
i = 0; tmp = inum;
while (tmp > 0) {
sprintf(&binnum[i],"%1d",tmp%2);
tmp = tmp/2;
i++;
}

/* Terminate the string */
binnum[i] = '\0';


/* Complete the expression */
numdigits = ceil(logf(inum)/logf(2)); /* this will tell us how digits "inum" has*/
printf("The number of digits is %d\n",numdigits); /* \n for line break, %d for integers */



printf("inum=%d, fnum=%f, inum in binary=%s\n",
inum,fnum,binnum);
return 0; /* return 0 is needed to prevent void error in code*/
}
17 changes: 17 additions & 0 deletions practical102/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
README

Folder contains two sources codes, Conversion.c and Sum.c

Conversion.c is compiled using gcc, and the output is called "conv".

-lm links the Math libary
-Wall shows any warnings

"numdigits" is an unused variable

There is no return at the of the Conversion.c code.

Execute Conversion.c using the function ./conv


Compile and run Sum.c using the command gcc
36 changes: 36 additions & 0 deletions practical102/Sum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>


int main(void) {
/* Declare variables */
int i;
float sum1, sum2, diff;


/* First sum */
sum1 = 0.0;
for (i=1; i<=1000; i++) { /* Start i at 1 and increse by i value */
sum1 += 1./i;
// sum1 = sum1 + 1./i


}


/* Second sum */
sum2 = 0.0;
for (i=1000; i>0; i--) { /* start i at 1000 and then decrease i */
sum2 += 1./i;
}

printf(" Sum1=%f\n",sum1);
printf(" Sum2=%f\n",sum2);

/* Find the difference */
diff = sum1 - sum2; //calculate the difference between sum1 and sum2

printf(" Difference between the two is %f\n",diff);

return 0; // return 0 is needed

}