0% found this document useful (0 votes)
11 views

Week5 in Class Coding Exercise

This document contains code snippets demonstrating various C programming concepts like data types, operators, loops, functions, and input/output. It includes examples of printing data type sizes, getting the current time, calculating the remainder of a division, using logical operators, validating test scores with if/else statements, using switch statements to print letter grades, summing numbers with for and while loops, getting confirmed input with do-while loops, calculating powers with nested for loops, and defining and calling functions.

Uploaded by

Zack Overfield
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Week5 in Class Coding Exercise

This document contains code snippets demonstrating various C programming concepts like data types, operators, loops, functions, and input/output. It includes examples of printing data type sizes, getting the current time, calculating the remainder of a division, using logical operators, validating test scores with if/else statements, using switch statements to print letter grades, summing numbers with for and while loops, getting confirmed input with do-while loops, calculating powers with nested for loops, and defining and calling functions.

Uploaded by

Zack Overfield
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

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 sec = t % 60;


printf("%d second\n",sec);
return 0;
}
Logical OR operator
#include <stdio.h>
#include <stdlib.h>

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);

if ((score < 0) || (score > 100)) {


printf("Invalid score %i\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);

if ((score < 0) || (score > 100)) {


printf("Invalid score %i\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~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)

//keep printing “*”

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

//Add its value to sum

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

//Add i to the sum

//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

//Ask for confirmation

} 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

//Multiply the base to the result

printf("Raise %f to the power %i: %f\n", base, exp, result);


return 0;
}
Function definition
#include <stdio.h>
#include <stdlib.h>
double power(double base, int exp); //declare power function
int main()
{
double base=0, result=1.0;
int exp;
// Get inputs
printf("Input base:"); scanf("%f",&base);
printf("Input power (non-negavie integer):"); scanf("%d", &exp);
// Check inputs
if (exp < 0) {
printf("Invalid power %i\n", exp);
return 1;
}
result = power(base, exp);
printf("Raise %f to the power %i: %f\n", base, exp, result);
return 0;
}
Power function cont.
double power(double base, int n)
{
double result = 1.0;
for (;n>0;n--) {
result*=base;
}
return result;
}
Course survey
• Socrative.com student login
– https://b.socrative.com/login/student/
– Classroom: ZHANG9126

You might also like