2.1
#include <stdio.h>
/* This function adds two numbers and returns the result */
float fonk_add( int x, float y ){
float z;
z = x + y;
return z;
}
int main(){
int a;
float b, sum;
a=5;
b=12.50;
sum = fonk_add( a, b); // sum = fonk_add( a, b)=z
printf("The addition of %d and %0.2f is %0.2f\n", a,b,sum);
return 0;
}
2.2
#include <stdio.h>
/* This function adds two numbers and prints the result */
void fonk_add( int x, float y ){
float z;
z = x + y;
printf("From the function z = %.2f\n",z) ;
}
int main(){
int a;
float b;
a=5;
b=12.50;
fonk_add( a, b);
return 0;
}
2.3
#include <stdio.h>
/* This function prints the message */
void fonk_message(){
printf("This is a message\n") ;
}
int main(){
fonk_message();
return 0;
}
2.4
#include <stdio.h>
/* An integer number a is divided by a float number b
numbers a and b are read from the standart input consecutively */
float fonk( int x, float y ){
float z;
z = x / y;
return z;
}
int main(){
int a;
float b, c;
while(1){
printf("Enter two numbers : integer a and float b ? : ");
scanf("%d %f",&a,&b);
c = fonk(a, b); // c = z
printf(" %d / %0.2f = %0.2f \n", a,b,c);
}
return 0;
}
2.5
#include <stdio.h>
int main(){
char c1;
char c2;
c1 = 'A';
c2 = 'a';
printf("Convert the value of c1 to character: %c.\n", c1);
printf("Convert the value of c2 to character: %c.\n", c2);
return 0;
}
2.6
/* 04L02.c: Convert numeric values back to characters */
#include <stdio.h>
int main(){
char c1;
char c2;
c1 = 65;
c2 = 97;
printf("The character that has the numeric value of 65 is: %c.\n", c1);
printf("The character that has the numeric value of 97 is: %c.\n", c2);
return 0;
}
2.7
/* 04L04.c: Floating-point operations */
#include <stdio.h>
int main(){
int int_num1, int_num2, int_num3, int_num4; /* Declare integer variables */
float flt_num1, flt_num2, flt_num3, flt_num4; /* Declare floating-point variables */
int_num1 = 32 / 10; /* Both divisor and dividend are integers */
flt_num1 = 32 / 10;
int_num2 = 32.0 / 10; /* The divisor is an integer */
flt_num2 = 32.0 / 10;
int_num3 = 32 / 10.0; /* The dividend is an integer */
flt_num3 = 32 / 10.0;
int_num4 = 32.0 / 10.0;
flt_num4 = 32.0 / 10.0;
printf("The integer divis. of 32/10 is: %d\n", int_num1);
printf("The floating-point divis. of 32/10 is: %f\n", flt_num1);
printf("The integer divis. of 32.0/10 is: %d\n", int_num2);
printf("The floating-point divis. of 32.0/10 is: %f\n", flt_num2);
printf("The integer divis. of 32/10.0 is: %d\n", int_num3);
printf("The floating-point divis. of 32/10.0 is: %f\n", flt_num3);
printf("The integer divis. of 32.0 / 10.0 is: %d\n", int_num4);
printf("The floating-point divis. of 32.0 / 10.0 is: %f\n", flt_num4);
return 0;
}
3.1
#include <stdio.h>
int main(){
char c1,c2,c3;
c1 = '?';
c2 = '$';
c3 = '\n';
printf("The numeric value of ? $ \\n are %d %d %d.\n", c1,c2,c3);
return 0;
3.2
/* 04L07.c: Reading a character from standart input by calling
getc() and outputting the character by putc() and putchar() */
#include <stdio.h>
int main(){
char ch;
printf ("Enter a single character ? : ");
ch=getc(stdin);
putchar(ch);
putchar('\n');
putc(ch,stdout);
putc('\n',stdout);
printf("%c\n",ch);
return 0;
}
3.3
/* 04L05.c: Reading a character from standart input by calling scanf()
and printing out the character and its numeric value */
#include <stdio.h>
int main(){
char ch;
printf ("Enter a single character ? : ");
scanf("%c",&ch);
printf("The numeric value of %c is : %d.\n",ch, ch);
return 0;
}
3.4
/* 04L06.c: Reading a character from standart input by calling getchar()
and outputting the character by putchar() */
#include <stdio.h>
int main(){
char ch;
printf ("Enter a single character ? : ");
ch=getchar();
putchar(ch);
putchar('\n');
return 0;
}
3.5
#include <stdio.h>
int main(){
int num1, num2;
float num3;
double num4 , num5 , num8;
long double num6 ,num7;
unsigned int num9;
num1 = 12;
num2 = 12345;
num3 = 30.456789;
num4 = 999999999999999.0;
num5 = 0.123456789123456;
num6 = 9223372036854775807;
num7 = 0.12345678912345671;
num8 = 9.567845e307;
num9 = 4220000000;
printf("%d %10d\n", num1,num2);
printf("%5d %-5d %05d\n", num1,num1,num1);
printf("%f %10.4f %.3f %10.2f\n", num3, num3, num3, num3);
printf(" %20.1f %30.15f\n", num4, num5);
printf(" %20.0Lf %30.17Lf\n", num6, num7);
printf(" %e %E\n", num8, num8);
printf(" %u \n", num9);
return 0;
}
3.6
/* 06L01.c: Use arithmetic assignment operators */
#include <stdio.h>
int main(){
int x, y, z;
x = 1;
y = 2;
printf("Given x = %d, and y = %d,\n", x, y);
x = x + y;
printf("x = x + y assigns %d to x.\n", x);
x = 1;
printf("Given x = %d, and y = %d,\n", x, y);
x += y; // x=x+y
printf("x += y assigns %d to x.\n", x);
x = 5;
printf("Given x = %d, and y = %d,\n", x, y);
x -= y; // x=x-y
printf("x -= y assigns %d to x. \n", x);
z = 10;
printf("Given z = %d, and y = %d,\n", z, y);
z /= y; // z=z/y
printf("z /= y assigns %d to z.\n", z);
z = 10;
printf("Given z = %d, x = %d ,and y = %d,\n", z, x, y);
z *= x + y; // z=z * (x + y)
printf("z *= x + y assigns %d to z.\n", z);
printf("Given x = %d ,and y = %d,\n", x, y);
x %= y; // x=x%y
printf("x %%= y assigns %d to x.\n", x);
return 0;
}
3.7
/* 06L02.c: pre- or post-increment(decreement) operators */
#include <stdio.h>
int main(){
int w, x, y, z, result;
w = x = y = z = 1; /* initialize x , y, w, and z */
printf("Given w = %d, x = %d, y = %d, and z = %d,\n", w, x, y, z);
result = ++w;
printf("++w gives: %d w=%d \n", result, w);
result = x++;
printf("x++ gives: %d x=%d \n", result, x);
result = --y;
printf("--y gives: %d y=%d \n", result, y);
result = z--;
printf("z-- gives: %d z=%d \n", result, z);
return 0;
}
Given w = 1, x = 1, y = 1, and z = 1,
++w gives: 2 w=2
x++ gives: 1 x=2
--y gives: 0 y=0
z-- gives: 1 z=0
3.8
#include <stdio.h>
int main(){
int x, y;
double z;
x = 7;
y = 25;
z = 24.5;
printf("Given x = %d, y = %d, and z = %.2f,\n", x, y, z);
printf("x >= y produces: %d\n", x >= y);
printf("x == y produces: %d\n", x == y);
printf("x < z produces: %d\n", x < z);
printf("y > z produces: %d\n", y > z);
printf("x != y - 18 produces: %d\n", x != y - 18);
printf("x + y != z produces: %d\n", x + y != z);
return 0;
}
3.9
#include <stdio.h>
int main(){
int x, y;
x = 7;
y = 5;
printf("Given x = %d, y = %d\n", x, y);
printf("x / y produces: %d\n", x / y);
printf("(float)x / y produces: %.3f\n", (float) x / y);
return 0;
}
4.1
#include <stdio.h>
int main(){
int a, b,c;
while(1){
printf("Enter three integers ? : ");
scanf("%d%d%d",&a,&b,&c);
printf("Given a = %d, b = %d, c = % d \n",a,b,c);
if ( a+b > c)
printf(" a+b > c \n");
if ( a+b < c)
printf(" a+b < c \n");
if ( a+b == c)
printf(" a+b = c \n");
}
return 0;
}
4.2
#include <stdio.h>
int main(){
int a;
while(1){
printf("Enter an integer number ? : ");
scanf("%d",&a);
if (a%2 == 0)
printf("%d is a even number\n", a);
else
printf("%d is a odd number\n", a);
}
return 0;
}
4.3
#include <stdio.h>
int main(){
int x;
printf("Enter an integer number ? : ");
scanf("%d",&x);
if (x > 0){
if (x%2 == 0)
printf("%d is an even number.\n", x);
else
printf("%d is an odd number.\n", x);
}
else if (x == 0)
printf("The number is zero.\n");
else
printf("%d is a negative number \n", x);
return 0;
}
4.4
#include <stdio.h>
int main(){
int n;
while(1){
printf("Enter an integer number ? : ");
scanf("%d",&n);
if (n%2 == 0 && n%3 == 0)
printf("%d can be divided by both 2 and 3\n", n);
else if (n%2 == 0 || n%3 == 0)
printf("%d can be divided by 2 or 3\n", n);
else
printf("%d can not be divided by both 2 and 3\n", n);
}
return 0;
}
4.5
#include <stdio.h>
int main(){
int a,b;
while(1){
printf("Enter two integers ? : ");
scanf("%d %d",&a,&b);
if ( (a-b) !=0 && (a+b)%3 == 0 && !((a+b) > 10) )
printf("%d %d\n", a,b);
}
return 0;
}
4.6
#include <stdio.h>
int main(){
int x;
printf("Enter an integer < 50 ? : ");
scanf("%d",&x);
if (x < 10)
printf(" %d is less than 10.\n",x);
else if (x < 20)
printf(" %d is less than 20.\n",x);
else if (x < 30)
printf(" %d is less than 30.\n",x);
else if (x < 40)
printf(" %d is less than 40.\n",x);
else if (x < 50)
printf(" %d is less than 50.\n",x);
else
printf("Wrong number!. Try again.\n");
return 0;
}
4.7
#include <stdio.h>
int main(){
char day;
printf("Please enter a single character a,b,c \n");
day = getchar();
switch (day){
case 'a':
printf("a\n");
case 'b':
printf("b\n");
case 'c':
printf("c\n");
}
return 0;
}
4.8
#include <stdio.h>
int main(){
int day;
printf("Please enter a single digit for a day\n");
printf("(within the range of 1 to 7):\n");
scanf("%d",&day);
switch (day){
case 1:
printf("Day 1 is Sunday.\n");
break;
case 2:
printf("Day 2 is Monday.\n");
break;
case 3:
printf("Day 3 is Tuesday.\n");
break;
case 4:
printf("Day 4 is Wednesday.\n");
break;
case 5:
printf("Day 5 is Thursday.\n");
break;
case 6:
printf("Day 6 is Friday.\n");
break;
case 7:
printf("Day 7 is Saturday.\n");
break;
default:
printf("The digit is not within the range of 1 to 7.\n");
break;
}
return 0;
}
4.9
#include <stdio.h>
int main()
{
int x=5,y=7,z=10,m;
m=x > 3 ? y : z;
// if x > 3 is true y value else z value will be assigned to m.
printf( " %d\n", m ); // since x > 3 is true y value will be assigned to m.
// The result of the expression ( x > 3 ? y : z ) can be printed directly
printf( " %d\n", x > 3 ? y : z );
x=2;
m=x > 3 ? y : z;
printf("%d\n", m ); // since x > 3 is false z value will be assigned to m.
return 0;
}
5.1
#include <stdio.h>
int main(){
int i,n=10,sum=0;
for (i=1; i<= n; i++){
sum+=i;
}
printf("Toplam = %d\n",sum);
return 0;
}
5.2
#include <stdio.h>
int main(){
int i, j;
for (i=0, j=8; i<8; i++, j--)
printf("%d + %d = %d\n", i, j, i+j);
return 0;
}
5.3
#include <stdio.h>
int main(){
int i, j;
for (i=0, j=1; i<8; i++, j++)
printf("%d - %d = %d\n", j, i, j-i);
return 0;
}
5.4
#include <stdio.h>
int main(){
char c;
printf("Enter a character:\n(enter x to exit)\n");
for ( ; c != 'x'; ) {
c = getc(stdin);
putchar(c);
}
printf("\nOut of the for loop. Bye!\n");
return 0;
}
5.5
#include <stdio.h>
int main(){
int c;
c = ' ';
printf("Enter a character:\n(enter x to exit)\n");
while (c != 'x') {
c = getc(stdin);
putchar(c);
}
printf("\nOut of the while loop. Bye!\n");
return 0;
}
5.6
#include <stdio.h>
int main(){
char i;
i = 65;
do {
printf("The numeric value of %c is %d.\n", i, i);
i++;
} while (i<72);
return 0;
}
5.7
#include <stdio.h>
int main(){
int i, j;
for (i=1; i<=3; i++) { /* outer loop */
printf("The start of iteration %d of the outer loop.\n", i);
for (j=1; j<=4; j++) /* inner loop */
printf(" Interation %d of the inner loop.\n", j);
printf("The end of iteration %d of the outer loop.\n", i);
}
return 0;
}
5.8
#include <stdio.h>
int main(){
int num=0;
for (; (num%2 == 0) || (num%3 == 0); ){
printf(" %d can be divided by 2 or 3\n", num);
printf("Enter an integer number ? : ");
scanf("%d",&num);
}
printf(" %d can not be divided by 2 or 3\n", num);
return 0;
}
5.9
#include<stdio.h>
int main(){
int i,j,x;
x=0;
for ( i=2; i < 5 ; i+=2 ){
for ( j=4; j > 0 ; j-- ){
if ( (i+j)%2==0 )
continue;
else {
x+=i+j;
printf(" i+j=%d \n",i+j);
}
}
}
printf(" x=%d \n",x);
return 0;
}
6.1
#include <stdio.h>
#include <math.h>
int main(){
double x;
x = 45.0; /* 45 degree */
x *= 3.141593 / 180.0; /* convert to radians */
printf("The sine of 45 is: %f.\n", sin(x));
printf("The cosine of 45 is: %f.\n", cos(x));
printf("The tangent of 45 is: %f. \n", tan(x));
return 0;
}
6.2
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
double x, y,m=2.0,n=-7.8 ;
int z;
x = 4.0;
y = 3.0;z=-7;
printf("The 4 x 4 x 4 is: %7.0f %.0f \n", pow(x, y),pow(4.0,3.0));
printf("The 3th root of 8 is : %7.4f \n",pow(2*x,1/y));
printf("The square root of 4 is: %2.0f %.0f \n", sqrt(x),sqrt(4));
printf("The absulute value of -7 and -7.8 are: %d %.2f\n", abs(z),fabs(n));
printf(" exp(1)=%f,exp(2)=%f,exp(3)=%f \n", exp(1),exp(2),exp(3));
printf("exp(pow(2,2))=%f\n",exp(pow(m,2))) ;
printf("log(100)=%f log10(100)=%f \n",log(100),log10(100));
return 0;
}
6.3
#include <stdio.h>
int main(){
int i;
printf("Integers that can be divided by both 2 and 3\n");
printf("(within the range of 0 to 100):\n");
for (i=0; i<=100; i++)
if ((i%2 == 0) && (i%3 == 0))
printf(" %d\n", i);
return 0;
}
6.4
#include <stdio.h>
int main(){
int i;
printf("Even Number Odd Number\n");
for (i=0; i<10; i++)
if (i%2 == 0)
printf("%d", i);
else
printf("%14d\n", i);
return 0;
}
6.5
#include <stdio.h>
int main(){
int i;
for (i=-5; i<=5; i++){
if (i > 0){
if (i%2 == 0)
printf("%d is an even number.\n", i);
else
printf("%d is an odd number.\n", i);
}else if (i == 0)
printf("The number is zero.\n");
else
printf("Negative number: %d\n", i);
}
return 0;
}
6.6
#include <stdio.h>
int main(){
int c;
printf("Enter a character:\n(enter x to exit)\n");
while (1) {
c = getc(stdin);
if (c == 'x')
break;
}
printf("Break the infinite while loop. Bye!\n");
return 0;
}
6.7
#include <stdio.h>
int main(){
int i, sum=0;
for (i=1; i<8; i++){
if ((i==3) || (i==5))
continue;
sum += i;
}
printf("The sum of 1, 2, 4, 6, and 7 is: %d\n", sum);
return 0;
}
6.8
#include<stdio.h>
int main(){
int i,j,x;
x=0;
for ( i=6; i >=2 ; i-=2 ){
for ( j=i+1; j > 2 ; j-- ){
if ( (i+j)%3!=0 )
continue;
else {
x+=i+j;
printf(" i+j=%d \n",i+j);
}
}
}
printf(" x=%d \n",x);
return 0; }
6.9
#include<stdio.h>
int main(){
int n,x,y;
n=-15;x=0;y=0;
while( n <= 25){
while( n < 0 && n%3==0 ){
printf(" %d \n",n);
x+=n;
break;
}
while( n > 0 && n%5==0){
printf(" %d \n",n);
y+=n;
break;
}
n+=2;
}
printf(" x=%d y=%d\n",x,y);
return 0;
}
6.10
#include <stdio.h>
double fonk(int n1, int n2){
int i;
double sum=0;
for (i=n1; i<=n2;i++){
sum+=i;
}
return sum;
}
int main(){
int n1,n2;
while(1){
printf("Enter two integer numbers n1 < n2: ");
scanf("%d%d",&n1,&n2);
if ( n1 > n2 ){
printf("n1 should be smaller than n2. TRY AGAIN!... \n");
continue;
}
printf("Toplam=%.0f\n", fonk(n1,n2) );
}
return 0;
}
7.1
/* 12L01.c: Initialize an array */
#include <stdio.h>
/*
ARRAY LIST
|---|---|---|---|---|---|---|---|---|----|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|----|
*/
int main(){
int i;
int list[10];
for (i=0; i<10; i++){
list[i] = i + 1;
printf( "list[%d] is initialized with %d.\n", i, list[i]);
}
return 0;
}
7.2
#include <stdio.h>
int main(){
char array_ch[7] = {'H', 'e', 'l', 'l', 'o', '!', '\0'};
int i;
for (i=0; i<7; i++)
printf("array_ch[%d] contains: %c\n", i, array_ch[i]);
/*--- method I ---*/
printf( "Put all elements together(Method I):\n");
for (i=0; i<7; i++)
printf("%c", array_ch[i]);
/*--- method II ---*/
printf( "\nPut all elements together(Method II):\n");
printf( "%s\n", array_ch);
return 0;
}
7.3
/* 12L05.c: Stop at the null character */
#include <stdio.h>
int main(){
char array_ch[15] = {'C', ' ',
'i', 's', ' ',
'p', 'o', 'w', 'e', 'r',
'f', 'u', 'l', '!', '\0'};
int i;
for (i=0; array_ch[i]; i++)
printf("%c", array_ch[i]);
printf("\n");
return 0;
}
7.4
/* 12L06.c: Print out a 2-D array */
#include <stdio.h>
/*
COL1 COL2 COL3 COL4 COL5
|-----|-----|-----|-----|-----|
ROW1 | 1 | 2 | 3 | 4 | 5 |
|-----|-----|-----|-----|-----|
ROW2 | 10 | 20 | 30 | 40 | 50 |
|-----|-----|-----|-----|-----|
ROW3 | 100 | 200 | 300 | 400 | 500 |
|-----|-----|-----|-----|-----|
*/
int main(){
int two_dim[3][5] = { {1, 2, 3, 4, 5},
{10, 20, 30, 40, 50},
{100, 200, 300, 400, 500} };
int i, j;
for (i=0; i<3; i++){
for (j=0; j<5; j++)
printf("%6d", two_dim[i][j]);
printf("\n");
}
return 0;
}
7.5
#include <stdio.h>
int main()
{
char array_ch[] = {'C', ' ',
'i', 's', ' ',
'p', 'o', 'w', 'e', 'r',
'f', 'u', 'l', '!', '\0'};
int list_int[][3] = {
{1, 1, 1},
{ 2, 2, 8},
{3, 9, 27},
{4, 16, 64},
{5, 25, 125},
{6, 36, 216},
{7, 49, 343} };
printf("The size of array_ch[] is %d bytes.\n", sizeof (array_ch));
printf("The size of list_int[][3] is %d bytes.\n", sizeof (list_int));
return 0;
}
7.6
/* 13L01.c: Initialize strings */
#include <stdio.h>
int main(){
char str1[] = {'A', ' ',
's', 't', 'r', 'i', 'n', 'g', ' ',
'c', 'o', 'n', 's', 't', 'a', 'n', 't', '\0'};
char str2[] = "Another string constant";
int i;
/* print out str1 */
for (i=0; str1[i]; i++)
printf("%c", str1[i]);
printf("\n");
/* print out str2 */
for (i=0; str2[i]; i++)
printf("%c", str2[i]);
printf("\n");
return 0;
}
7.7
/* 13L02.c: Measure string length */
#include <stdio.h>
#include <string.h>
int main(){
char str1[] = {'A', ' ',
's', 't', 'r', 'i', 'n', 'g', ' ',
'c', 'o', 'n', 's', 't', 'a', 'n', 't', '\0'};
char str2[] = "Another string constant";
printf("The length of str1 is: %d bytes\n", strlen(str1));
printf("The length of str2 is: %d bytes\n", strlen(str2));
return 0;
}
7.8
#include <stdio.h>
#include <string.h>
int main(){
char str1[] = "Copy a string.";
char str2[15];
char str3[15];
int i;
/* with strcpy() */
strcpy(str2, str1);
/* without strcpy() */
for (i=0; str1[i]; i++)
str3[i] = str1[i];
str3[i] = '\0';
/* display str2 and str3 */
printf("The content of str2: %s\n", str2);
printf("The content of str3: %s\n", str3);
return 0;
}
7.9
#include <stdio.h>
int main(){
char str[5];
int i;
printf("Enter a string ? : ");
gets( str );
// scanf("%s",str);
i = 0;
while (str[i]){
if ((str[i] >= 97) && (str[i] <= 122))
str[i] -= 32; /* convert to upper case */
i++;
}
printf("The entered string is (in uppercase):\n");
puts( str );
// printf("%s\n",str);
return 0;
}
7.10
#include<stdio.h>
int fonk(int arr1[],int arr2[]){
int i,s=0;
for ( i = 0; i < 6 ; i++)
if (arr1[i]%2==0)
arr2[s++]= arr1[i];
return s;
}
int main(){
int n,i;
int arr1[]={2,3,4,6,7,12};
int arr2[10];
n=fonk(arr1,arr2);
for (i=0; i < n ; i++)
printf("%3d ",arr2[i]);
printf("\n");
return 0;
}
7.11
#include<stdio.h>
int main(){
int i,j,s=0;
int arr1[2][3]={{2,3,4},{6,7,12}};
int arr2[10];
for ( i = 0; i < 2 ; i++)
for ( j = 0; j < 3 ; j++)
if (arr1[i][j]%2==0)
arr2[s++]=arr1[i][j];
for (i=0; i < s ; i++)
printf("%3d ",arr2[i]);
printf("\n");
return 0;
}
8.1
/* 11L01.c: Obtain addresses */
#include <stdio.h>
int main()
{
char c;
int x;
float y;
printf("c: address=%p, content=%c\n", &c, c);
printf("x: address=%p, content=%d\n", &x, x);
printf("y: address=%p, content=%5.2f\n", &y, y);
c = 'A';
x = 7;
y = 123.45;
printf("c: address=%p, content=%c\n", &c, c);
printf("x: address=%p, content=%d\n", &x, x);
printf("y: address=%p, content=%5.2f\n", &y, y);
return 0;
}
8.2
/* 11L02.c: Declare and assign values to pointers */
#include <stdio.h>
int main()
{
char c, *ptr_c;
int x, *ptr_x;
float y, *ptr_y;
c = 'A';
x = 7;
y = 123.45;
printf("c: address=%p, content=%c\n", &c, c);
printf("x: address=%p, content=%d\n", &x, x);
printf("y: address=%p, content=%5.2f\n", &y, y);
ptr_c = &c;
printf("ptr_c: address=%p, content=%p\n", &ptr_c, ptr_c);
printf("*ptr_c => %c\n", *ptr_c);
ptr_x = &x;
printf("prt_x: address=%p, content=%p\n", &ptr_x, ptr_x);
printf("*ptr_x => %d\n", *ptr_x);
ptr_y = &y;
printf("prt_y: address=%p, content=%p\n", &ptr_y, ptr_y);
printf("*ptr_y => %5.2f\n", *ptr_y);
return 0;
}
8.3
/* 11L03.c: Change values via pointers */
#include <stdio.h>
int main()
{
char c, *ptr_c;
c = 'A';
printf("c: address=%p, content=%c\n", &c, c);
ptr_c = &c;
printf("ptr_c: address=%p, content=%p\n", &ptr_c, ptr_c);
printf("*ptr_c => %c\n", *ptr_c);
*ptr_c = 'B';
printf("ptr_c: address=%p, content=%p\n", &ptr_c, ptr_c);
printf("*ptr_c => %c\n", *ptr_c);
printf("c: address=%p, content=%c\n", &c, c);
return 0;
}
8.4
/* 11L04.c: Pointing to the same thing */
#include <stdio.h>
int main()
{
int x=1234;
int *ptr_1, *ptr_2, *ptr_3;
printf("x: address=%p, content=%d\n", &x, x);
ptr_1 = &x;
printf("ptr_1: address=%p, content=%p\n", &ptr_1, ptr_1);
printf("*ptr_1 => %d\n", *ptr_1);
ptr_2 = &x;
printf("ptr_2: address=%p, content=%p\n", &ptr_2, ptr_2);
printf("*ptr_2 => %d\n", *ptr_2);
ptr_3 = &x;
printf("ptr_3: address=%p, content=%p\n", &ptr_3, ptr_3);
printf("*ptr_3 => %d\n", *ptr_3);
return 0;
}
8.5
/* 12L03.c: Reference an array by a pointer */
#include <stdio.h>
int main()
{
int *ptr_int;
int list_int[10];
int i;
for (i=0; i<10; i++)
list_int[i] = i + 1;
ptr_int = list_int;
printf( "The start address of the array: %p\n", ptr_int);
printf( "The value of the first element: %d \n", *ptr_int);
ptr_int = &list_int[0];
printf( "The address of the first element: %p\n", ptr_int);
printf( "The value of the first element: %d\n", *ptr_int);
printf( "The value of the last element: %d\n", *(ptr_int+9) );
return 0;
}
8.7
#include<stdio.h>
float f_sum(int arr1[],int m){
int i;
float sum_arr1=0;
for ( i = 0; i < m ; i++)
sum_arr1 += arr1[i];
return sum_arr1;
}
int main(){
int n;
int arr1[] = {2,3,4,6};
n=4;
printf("Dizi toplami=%.0f dizi ort=%.2f\n",f_sum(arr1,n),f_sum(arr1,n)/n);
return 0;
}
8.8
#include<stdio.h>
void f_sum(int arr1[],int m){
float sum_positive=0,sum_negative=0;
int i, say1=0, say2=0;
for ( i = 0; i < m ; i++){
if (arr1[i] >= 0){
sum_positive += arr1[i];
say1++;
}
else{
sum_negative += arr1[i];
say2++;
}
}
printf("sum_positive = %3.0f sum_positive_ort = %2.0f \n",sum_positive,
sum_positive/say1);
printf("sum_negative = %3.0f sum_negative_ort = %2.0f \n",sum_negative,
sum_negative/say2);
}
int main(){
int n;
int arr1[] = { -4,3,4,-5,5,-3,0};
n=7;
f_sum(arr1,n);
return 0;
}
8.9
/* Use POINTERS */
#include<stdio.h>
void f_sum(int *arr1,float *mean1,float *mean2){
float sum_positive=0,sum_negative=0;
int i, say1=0, say2=0;
for ( i = 0; i < 7 ; i++){
if (arr1[i] >= 0){
sum_positive += arr1[i];
say1++;
}
else{
sum_negative += arr1[i];
say2++;
}
}
*mean1=sum_positive/say1;
*mean2=sum_negative/say2;
}
int main(){
int *ptr_arr1;
float *ptr_mean1,*ptr_mean2,ort1,ort2;
int arr1[] = { -4,3,4,-5,5,-3,0};
ptr_mean1=&ort1;
ptr_mean2=&ort2;
ptr_arr1=arr1;
f_sum(ptr_arr1,ptr_mean1,ptr_mean2);
printf("sum_positive_ort = %2.0f \n",ort1);
printf("sum_negative_ort = %2.0f \n",ort2);
return 0;
}
8.10
#include<stdio.h>
int fonk( int arr1[] ,int arr2[][3]){
int i,j,s=0;
for ( i = 0; i < 4 ; i++){
for ( j = 0; j < 3 ; j++){
if (arr2[i][j]%3==0)
arr1[s++]=arr2[i][j];
}
}
return s;
}
int main(){
int i,arr1_size;
int arr1[20];
int arr2[4][3]={{2,3,4},{6,9,2},{12,4,7},{1,15,8}};
arr1_size=fonk(arr1,arr2);
printf("\nARR1 dizisi :");
for ( i = 0; i < arr1_size ; i++)
printf("%3d",arr1[i]);
printf("\n");
return 0;
}
09.01
/* Program scope vs block scope */
#include <stdio.h>
int x = 1234; /* program scope */
double y = 1.234567; /* program scope */
void function_1(){
printf("From function_1:\n x=%d, y=%f\n", x, y);
}
int main(){
int x = 4321; /* block scope 1*/
function_1();
printf("Within the main block:\n x=%d, y=%f\n", x, y);
/* a nested block */
{
double y = 7.654321;
function_1();
printf("Within the nested block:\n x=%d, y=%f\n", x, y);
}
return 0;
}
09.02
/* Use the static specifier */
#include <stdio.h>
int add_two(int x, int y){
static int counter = 1;
// int counter = 1;
printf("This is the function call of %d,\n", counter++);
return (x + y);
}
int main(){
int i, j;
for (i=0, j=5; i<5; i++, j--)
printf("the addition of %d and %d is %d.\n\n", i, j, add_two(i, j));
return 0;
}
09.03
#include<stdio.h>
#include<stdlib.h>
int main(){
const int x=15;
const int arr[]={3,4,7,2,5};
arr[1]=6; // It is wrong since arr has a const modifier
x=7; // It is wrong since x has a const modifier
printf("%d %5d\n",x,arr[1]);
return 0;
}
09.04
/* Access to arrays via pointers */
#include <stdio.h>
int main()
{
char str[] = "It's a string!";
char *ptr_str;
int list[] = {1, 2, 3, 4, 5};
int *ptr_int;
/* access to char array */
ptr_str = str;
printf("Before the change, str contains: %s\n", str);
printf("Before the change, str[5] contains: %c\n", str[5]);
*(ptr_str + 5) = 'A';
printf("After the change, str[5] contains: %c\n", str[5]);
printf("After the change, str contains: %s\n", str);
/* access to int array */
ptr_int = list;
printf("Before the change, list[2] contains: %d\n", list[2]);
*(ptr_int + 2) = -3;
printf("After the change, list[2] contains: %d\n", list[2]);
return 0;
}
09.05
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fonk( char *str1[], char str2[][15]){
int i,j;
for (i=0; i < 3; i++) {
strcpy( str2[i],str1[i]);
for (j=0; str2[i][j]; j++)
str2[i][j]-=32;
}
}
int main(){
char *str1[3]= {"istanbul","ankara","diyarbakir"};
char str2[3][15];
int i;
fonk(str1,str2);
for (i=0; i < 3; i++)
printf("%s \n",str2[i]);
return 0;
}
09.06
// Accessing a function via pointer
#include<stdio.h>
int multiply (int c,int d){
return c*d;
}
int main() {
int (*ptr_f)(int,int); //declare a pointer *ptr_f to a function multiply
int result,x,y; // with two integer arguments
ptr_f=multiply; //
printf("Enter two integers x and y : ");
scanf("%d%d",&x,&y);
result= (ptr_f)(x,y); // Accessing a function via pointer
printf("%d \n",result);
return 0;
}
09.07
/* Recursive factorial function */
#include <stdio.h>
int factor( int );
int main()
{
int i;
for ( i = 1; i <= 10; i++ )
printf( "%2d! = %d\n", i, factor( i ) );
return 0;
}
// 5! 120
int factor( int number ){ // 5*f(4) 5*24=120
if ( number <= 1 ) // base cases // 4*f(3) 4*6= 24
return 1; // 3*f(2) 3*2= 6
else // 2*f(1) 2*1= 2
return ( number * factor(number - 1)); // 1 1 1
}
10.01
/* Read and write one character at a time */
#include <stdio.h>
void CharReadWrite(FILE *fin, FILE *fout);
int main(){
FILE *fptr1, *fptr2;
fptr1 = fopen("input.f","r");
fptr2 = fopen("output.f", "w");
CharReadWrite(fptr1, fptr2);
fclose(fptr1);
fclose(fptr2);
return 0;
}
/* function definition */
void CharReadWrite(FILE *fin, FILE *fout){
char c;
while ((c=fgetc(fin)) != EOF){
putchar(c);
fputc(c, fout);
}
putchar('\n');
}
10.02
/* CHAR SAYISINI BUL */
#include <stdio.h>
void Charfind(FILE *fin);
int main(){
FILE *fptr1;
fptr1 = fopen("inf","r");
Charfind(fptr1);
fclose(fptr1);
return 0;
}
/* function definition */
void Charfind(FILE *fin){
int ch;
int arr[300]={0},i;
while ((ch=fgetc(fin)) != EOF){
arr[ch]+=1;
}
for (i=65;i<= 122; i++){
if (arr[i]==0) continue;
printf("%c %2d\n",i,arr[i]);
}
}
10.03
#include <stdio.h>
int main(){
int num,notu;
char adi[15];
char soyadi[15];
FILE *fptr1;
//BASLIK
printf("NO ADI SOYADI NOTU \n");
printf("-- --- ------ ---- \n");
fptr1 = fopen("pzt.data","r") ;
while(!feof(fptr1)){
fscanf(fptr1,"%d %s %s %d ",&num,adi, soyadi, ¬u);
printf("%2d %-12s %-14s %4d \n",num,adi,soyadi,notu);
}
return 0;
}
10.04
#include <stdio.h>
#include <string.h>
int main(){
int num;
char adi[15];
char soyadi[15];
char bolumu[10];
FILE *fptr;
fptr = fopen("pers.f","w") ;
while(1){
printf("Bolum No'su giriniz ? : ");
scanf("%d",&num);
if (num== 0) break;
printf("Adini giriniz ? : ");
scanf("%s",adi);
printf("Soyadini giriniz ? : ");
scanf("%s",soyadi);
printf("Bolum adini giriniz ? : ");
scanf("%s",bolumu);
fprintf(fptr,"%4d %-10s %-10s %-10s\n",num,adi, soyadi, bolumu);
}
fclose(fptr);
fptr = fopen("pers.f","r") ;
while(!feof(fptr)){
fscanf(fptr,"%d %s %s %s\n",&num,adi, soyadi, bolumu);
printf("No'su : %d\n",num);
printf("Adi : %s\n",adi);
printf("Soyadi: %s\n",soyadi);
printf("Bolumu: %s\n",bolumu);
printf("\n");
}
return 0;
}
10.05
#include <stdio.h>
#include <string.h>
int main(){
int i,s=0,say=0;
char name_arr[50][20], name[20];
FILE *fptr1;
fptr1 = fopen("name.f","r") ;
while(!feof(fptr1)){
fscanf(fptr1,"%s",name);
strcpy (name_arr[s],name);
s+=1;
}
while(1){
printf("Enter a name ? : ");
scanf("%s",name);
for (i=0 ; i<s; i++){
if (strcmp(name,name_arr[i])==0)
say+=1;
}
if (say > 0 ){
printf("%-10s %d\n",name,say);
say=0;
}
else
printf("%s not found\n",name);
}
return 0;
}
10.06
#include <stdio.h>
int main(){
int i=0,s=0;
int arr[50];
FILE *fptr1;
fptr1 = fopen("num.data","r") ;
while(!feof(fptr1)){
fscanf(fptr1,"%d",&arr[s]);
s++;
}
for (i=0; i < s;i++)
printf(" %d ",arr[i]);
printf("\n");
return 0;
}
10.07
#include <stdio.h>
int main(){
int i=0,m,n;
int arr[10][3];
FILE *fptr1;
fptr1 = fopen("num.data","r") ;
while(!feof(fptr1)){
arr[i][0]= arr[i][1]= arr[i][2]=0;
fscanf(fptr1,"%d%d%d",&arr[i][0], &arr[i][1], &arr[i][2]);
i+=1;
}
for (m=0; m < i;m++){
for (n=0; n < 3; n++)
printf("%4d",arr[m][n]);
printf("\n");
}
return 0;
}
10.08
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define size 6
int main(){
int i;
char *city_arr[]={"ankara","adana","kars","van","izmir","ordu"};
char city[20];
while(1){
printf("Enter a city name (to exit enter 0) : ? ");
scanf("%s",city);
if (city[0]=='0') exit(0);
for (i=0 ; i < size; i++){
if (strcmp(city,city_arr[i])==0)
break;
}
if ( i==size )
printf("%s NOT FOUND\n" ,city);
else
printf("%s FOUND\n",city_arr[i]);
}
return 0;
}
11.01
#include <stdio.h>
struct personel {
int num;
char adi[15];
char soyadi[15];
char bolumu[10];
} pers;
int main(){
FILE *fptr1;
fptr1=fopen("pers.f","w");
while(1){
printf("Numarasini giriniz ?\n");
scanf("%d",&pers.num);
if (pers.num == 0) break;
printf("Adini giriniz ?\n");
scanf("%s",pers.adi);
printf("Soyadini giriniz ?\n");
scanf("%s",pers.soyadi);
printf("Bolumunu giriniz ?\n");
scanf("%s",pers.bolumu);
fprintf(fptr1,"%3d %-10s %-10s %-10s\n",pers.num,pers.adi,
pers.soyadi,pers.bolumu);
}
fclose(fptr1);
fptr1=fopen("pers.f","r");
while(!feof(fptr1)){
fscanf(fptr1,"%d %s %s %s\n",&pers.num,pers.adi,pers.soyadi,
pers.bolumu);
printf("No'su : %d\n",pers.num);
printf("Adi : %s\n",pers.adi);
printf("Soyadi: %s\n",pers.soyadi);
printf("Bolumu: %s\n",pers.bolumu);
printf("\n");
}
return 0;
}
11.02
#include <stdio.h>
#include <string.h>
struct personel {
int id ;
char ad[15];
char soyad[15];
int sinav;
} rec[100];
int main(){
FILE *fptr1;
char adi[15],soyadi[15];
int i=0,k;
fptr1=fopen("pzt.data","r");
while(!feof(fptr1)){
fscanf(fptr1,"%d%s%s%d",&rec[i].id,rec[i].ad,rec[i].soyad,
&rec[i].sinav);
i+=1;
}
fclose(fptr1);
while(1){
printf("Adini giriniz ? : ");
scanf("%s",adi);
if (adi[0]=='0') break;
printf("Soyadini giriniz ? : ");
scanf("%s",soyadi);
for (k=0; k < i ; k++){
if ( strcmp(rec[k].ad, adi)==0 && strcmp(rec[k].soyad,soyadi)==0){
printf ("Adi:%s Soyadi:%s Sinav:%2d\n",rec[k].ad,
rec[k].soyad,rec[k].sinav);
break;
}
}
if (k==i) printf ("%s %s bulunamadi!!!\n", adi,soyadi);
}
return 0;
}
11.03
#include <stdio.h>
#include <string.h>
struct student {
int id ;
char ad[10];
} rec1;
struct ogren {
int id ;
char ad[10];
};
typedef struct ogr {
int id ;
char ad[10];
} rec4,rec5;
int main(){
rec1.id=20;
strcpy (rec1.ad, "Ali");
printf (" id=%3d Adi: %s \n", rec1.id,rec1.ad);
struct student rec2;
rec2.id=30;
strcpy (rec2.ad, "Veli");
printf (" id=%3d Adi: %s \n", rec2.id,rec2.ad);
typedef struct ogren record;
record rec3;
rec3.id=40;
strcpy(rec3.ad,"kemal");
printf (" id=%3d Adi: %s \n", rec3.id,rec3.ad);
rec4 kayit; //or ( rec5 kayit;)
kayit.id=50;
strcpy (kayit.ad,"osman");
printf (" id=%3d Adi: %s \n", kayit.id,kayit.ad);
return 0;
}
11.04
#include <stdio.h>
#include <stdlib.h>
int main(){
int *x;
char *ch;
x=malloc( sizeof(int));
ch=malloc( sizeof(char));
*x=25;
*ch='B';
printf("%4d %c\n",*x, *ch);
free(x);
free(ch);
return 0;
}
11.05
/* Dynamic memory allocation for an one dimensional
integer array with m elements */
#include <stdio.h>
#include <stdlib.h>
int main(){
int i,m,k=1;
int *arr;
printf ("Enter the array size : ? " );
// m is the array size
scanf("%d",&m);
//allocate memory for m integers with malloc
arr=malloc(m * sizeof(int));
// initialize arr's elements with values starting from 1 to m
for (i=0; i < m; i++)
arr[i]=k++;
printf("\n");
// print arr
for (i=0; i < m; i++)
printf("%10d",arr[i]);
printf("\n");
free(arr);
return 0;
}
12.01
/* Command line arguments */
#include <stdio.h>
#include <stdlib.h>
int main (int args, char *str[]){
int i;
int x,top=0;
printf("The value received by args is %d\n", args);
printf("There are %d command-line arguments passed to main():\n", args);
printf("The first command-line argument is: %s\n", str[0]);
printf("The rest command-line arguments are:\n");
for (i=1; i < args; i++){
x=atoi(str[i]); // x=atof(str[i]);
printf("%2d\n", x);
top+=x;
}
printf("top=%d\n",top);
return 0;
}
12.02
/* Command line arguments */
#include <stdio.h>
int main (int args, char *str[]){
FILE *ptr;
char adi[15],soyadi[15];
int num,ksin1, ksin2;
ptr=fopen(str[1],"r");
while(!feof(ptr)){
fscanf(ptr,"%d%s%s%d%d",&num,adi,soyadi,&ksin1,&ksin2);
printf("%3d %-12s %-12s %3d %3d\n", num,adi,soyadi,ksin1,ksin2);
}
return 0;
}
12.03
/* Command line arguments */
#include <stdio.h>
#include <stdlib.h>
int main (int args, char *str[]){
int x,y,sw;
sw=atoi(str[1]);
x=atoi(str[2]); // x=atof(str[i]);
y=atoi(str[3]);
switch (sw){
case 1 :
printf("%d + %d = %2d\n",x,y,x+y);
break;
case 2 :
printf("%d - %d = %2d\n",x,y, x-y);
break;
case 3 :
printf("%d * %d = %2d\n",x,y, x*y);
break;
default:
printf("Wrong argument!. First argument must be 1,2 or 3 \n");
}
return 0;
}
12.04
/* Command line arguments */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int args, char *str[]){
float x,y;
x=atof(str[2]);
y=atof(str[3]);
if (strcmp( str[1],"topla")==0)
printf("%.2f + %.2f = %.2f\n",x,y,x+y);
else if (strcmp( str[1],"cikar")==0)
printf("%.2f - %.2f = %.2f\n",x,y, x-y);
else if (strcmp( str[1],"carp")==0)
printf("%.2f * %.2f = %.2f\n",x,y, x*y);
else if (strcmp( str[1],"bol")==0)
printf("%.2f / %.2f = %.2f\n",x,y, x/y);
else
printf ("Eksik ya da yanlis arguman girdiniz!!!\n");
return 0;
}
12.05
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str1[]="Finaller Yaklasiyor";
char *str2;
str2=malloc( strlen(str1)+1 );
strcpy (str2,str1);
printf("%s %s\n",str1,str2);
free(str2);
return 0;
}