Week5 in Class Coding Exercise
Week5 in Class Coding Exercise
CS223
C coding styles
• Tab spaces: 4 or 8
• Styels:
– K&R
– GNU
– Allman
– …..
https://en.wikipedia.org/wiki/Indent_style
Print data type size
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("sizeof byte :%i bytes\n",sizeof(char));
printf("sizeof short :%i bytes\n",sizeof(short));
printf("sizeof int :%i bytes\n",sizeof(int));
printf("sizeof int* :%i bytes\n",sizeof(int*));
printf("sizeof long :%i bytes\n",sizeof(long));
printf("sizeof long long:%i bytes\n",sizeof(long long));
printf("sizeof float :%i bytes\n",sizeof(float));
printf("sizeof double :%i bytes\n",sizeof(double));
printf("sizeof long double:%i bytes\n",sizeof(long double));
printf("Hello world!\n");
return 0;
}
Get time
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t t;
t = time(0);
printf("%d seconds since 00:00:00 1970/01/01\n",t);
return 0;
}
Get second
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
time_t t = time(0);
int main()
{
printf("false OR false = %d\n", 0 || 0);
printf("false OR true = %d\n", 0 || 1);
printf("true OR false = %d\n", 1 || 0);
printf("true OR true = %d\n", 1 || 1);
printf("Done!\n");
return 0;
}
Logical OR operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("false or false = %s\n", (0 || 0)?"true":"false");
printf("false or true = %s\n", (0 || 1)?"true":"false");
printf("true or false = %s\n", (1 || 0)?"true":"false");
printf("true or true = %s\n", (1 || 1)?"true":"false");
printf("Done!\n");
return 0;
}
Logical AND operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("false AND false = %s\n", (0 && 0)?"true":"false");
printf("false AND true = %s\n", (0 && 1)?"true":"false");
printf("true AND false = %s\n", (1 && 0)?"true":"false");
printf("true AND true = %s\n", (1 && 1)?"true":"false");
printf("Done!\n");
return 0;
}
Pass or fail
#include <stdio.h>
#include <stdlib.h>
int main()
{
int score=0;
printf("Input score:");
scanf("%d", &score);
if (score < 60 ) {
printf("Fail\n");
} else { // score >= 60
printf("Pass\n");
}
printf("Done!\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int score=0;
printf("Input score [0~100]:");
scanf("%d", &score);
if (score < 0) {
printf("Invalid score %i, cannot be negative\n", score);
} else {
if (score > 100) {
printf("Invalid score %i, cannot be over 100\n", score);
} else {
if (score < 60 ) {
printf("Fail\n");
} else { // score >=60 AND score <= 100
printf("Pass\n");
}
}
}
printf("Done!\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int score=0;
printf("Input score [0~100]:");
scanf("%d", &score);
int main()
{
int score=0;
printf("Input score [0~100]:");
scanf("%d", &score);
printf("Done!\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int score=0;
printf("Input score [0~4]:");
scanf("%d", &score);
switch (score) {
case 4: printf("A - Excellent\n");
break;
case 3: printf("B – Good\n");
break;
case 2: printf("C - Qualified\n");
break;
case 1: printf("D - Pass\n");
break;
case 0: printf("F - Fail\n");
break;
default:printf("Invalid input:%i\n", score);
}
return 0;
}
For loop – print a line
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0, length=20;
printf("Input length:"); scanf("%d",&length);
//Print ‘*’ for a number of times to form a line
//Start i from 0 to length(init;test;incr)
return 0;
}
For loop – compute sum
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i = 0, sum=0, number=10;
printf("Input number:"); scanf("%d",&number);
//compute sum of numbers 0,1,2,3,…,number
//Start from 0 to number
printf("=%d\n",sum);
return 0;
}
While loop – compute sum
#include <stdio.h>
#include <stdlib.h>
int main()
{ int i = 0, sum=0, number=10;
printf("Input number:"); scanf("%d",&number);
// compute sum of numbers:0,1,2,3,……,number
// Repeat if i <= number
//Increase i by 1;
printf("=%d\n",sum);
return 0;
}
Do-While loop – confirmed
input
#include <stdio.h>
#include <stdlib.h>
int main()
{ int input_number=0; char answer=‘Y’; // answer Y or N (y or n)
//Repeat getting an integer, until confirmed
do {
//Input an integer number
} while ( );
//Repeat if answer is not Y or y
printf("=%d\n",sum);
return 0;
}
For loop – compute power
#include <stdio.h>
#include <stdlib.h>
int main()
{
double base=0, result=1.0;
int i, power;
printf("Input base:"); scanf("%f",&base);
printf("Input power (non-negavie integer):"); scanf("%d", &exp);
if (exp < 0) {// Check inputs
printf("Invalid power %i\n", exp);
return 1;
}
// Raise base to the power of exp
// Multiply the base number for power times
// Start i from 0 to power