Skip to content
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
Binary file modified Joseph Njoroge/Chapter 10/task1.exe
Binary file not shown.
Binary file modified Joseph Njoroge/Chapter 13/task1.exe
Binary file not shown.
Binary file modified Joseph Njoroge/Chapter 13/task2.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions Joseph Njoroge/Chapter 13/task3.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ int main(void)
char str[80];
int i, delt = 'A' - 'a';

printf("Enter a string less than *0 chars log: \n");
printf("Enter a string less than 80 chars log: \n");
gets( str );
i = 0;
while (str[i])
{
if ((str[i] >= 'a') && (str[i] <= 'Z'))
str[i] -= delt; //converts to uppercase.
str[i] += delt; //converts to uppercase.
++i;
}
printf("The entred string is: \n");
Expand Down
Binary file modified Joseph Njoroge/Chapter 13/task3.exe
Binary file not shown.
5 changes: 2 additions & 3 deletions Joseph Njoroge/Chapter 13/task4.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ int main(void)
{
int x , y;
printf("Enter the 2 values: \n");
scanf("%d %d, &x, &y");

printf("The sum of the ints are: %d", x + y);
scanf("%d %d", &x, &y);
printf("The sum of the ints are: %d\n", x + y);
return 0;
}
Binary file modified Joseph Njoroge/Chapter 13/task4.exe
Binary file not shown.
49 changes: 49 additions & 0 deletions Joseph Njoroge/Chapter 15/task1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <stdio.h>
#include <time.h>
/**
1. Rewrite the program in Listing 15.2. This time use the format specifier %c, instead
of %s, to print out the character string of the local time on your computer

include <stdio.h>
#include <time.h>
void GetDateTime(void);
main()
{
printf(“Before the GetDateTime() function is called.\n”);
GetDateTime();
printf(“After the GetDateTime() function is called.\n”);
return 0;
}
/* GetDateTime()
void GetDateTime(void)
{
time_t now;

printf(“Within GetDateTime().\n”);
time(&now);
printf(“Current date and time is: %s\n”,
asctime(localtime(&now)));
}
*/
void GetDateTime(void);


int main(void)
{
printf("Before the GetDateTime() function is called.\n");
GetDateTime();
printf("After the GetDateTime() function is called.\n");
return 0;

}


void GetDateTime(void)
{
time_t now;

printf("Within GetDateTime().\n");
time(&now);
printf("Current date and time is: %s\n",
asctime(localtime(&now)));
}
Binary file added Joseph Njoroge/Chapter 15/task1.exe
Binary file not shown.
23 changes: 23 additions & 0 deletions Joseph Njoroge/Chapter 15/task2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <stdio.h>

/**
2. Declare and define a function, called MultiTwo(), that can perform multiplication
on two integer variables. Call the MultiTwo() function from the main() function
and pass two integers to MultiTwo(). Then print out the result returned by the
MultiTwo() function on the screen.
*/
int Multitwo(int, int);
int main(void)
{
int a;
int b;

a = 9, b = 8;
printf("The multiplication of a and b is: %d\n", Multitwo(a, b));
return 0;
}

int Multitwo(int x, int y)
{
return x * y;
}
Binary file added Joseph Njoroge/Chapter 15/task2.exe
Binary file not shown.
86 changes: 86 additions & 0 deletions Joseph Njoroge/Chapter 15/task3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
3. Rewrite the program in Listing 15.3. This time, make a function that takes a variable
number of int arguments and performs the operation of multiplication on
these arguments.

15L03.c: Processing variable arguments
#include <stdio.h>
#include <stdarg.h>
double MultDouble(int x, ...);
main ()
{
double d1 = 1.5;
double d2 = 2.5;
double d3 = 3.5;
double d4 = 4.5;

printf(“Given an argument: %2.1f\n”, d1);
printf(“The result returned by MultDouble() is: %2.1f\n\n”,
MultDouble(1, d1));
printf(“Given arguments: %2.1f and %2.1f\n”, d1, d2);
printf(“The result returned by MultDouble() is: %2.1f\n\n”,
MultDouble(2, d1, d2));
printf(“Given arguments: %2.1f, %2.1f and %2.1f\n”, d1, d2, d3);
printf(“The result returned by MultDouble() is: %2.1f\n\n”,
MultDouble(3, d1, d2, d3));
printf(“Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n”,
d1, d2, d3, d4);
printf(“The result returned by MultDouble() is: %2.1f\n”,
MultDouble(4, d1, d2, d3, d4));
return 0;
}
//definition of MultDouble()
double MultDouble(int x, ...)
{
va_list arglist;
int i;
double result = 0.0;

printf(“The number of arguments is: %d\n”, x);
va_start (arglist, x);
for (i=0; i<x; i++)
result += va_arg(arglist, double);
va_end (arglist);
return result;
}
*/

#include <stdio.h>
#include <stdarg.h>
double MultDouble(int x, ...);
int main()
{
double d1 = 6.5;
double d2 = 2.9;
double d3 = 3.5;
double d4 = 4.8;

printf("Given an argument: %2.1f\n", d1);
printf("The result returned by MultDouble() is: %2.1f\n\n",
MultDouble(1, d1));
printf("Given arguments: %2.1f and %2.1f\n", d1, d2);
printf("The result returned by MultDouble() is: %2.1f\n\n",
MultDouble(2, d1, d2));
printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3);
printf("The result returned by MultDouble() is: %2.1f\n\n",
MultDouble(3, d1, d2, d3));
printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n",
d1, d2, d3, d4);
printf("The result returned by MultDouble() is: %2.1f\n",
MultDouble(4, d1, d2, d3, d4));
return 0;
}
//definition of MultDouble()
double MultDouble(int x, ...)
{
va_list arglist;
int i;
double result = 0.0;

printf("The number of arguments is: %f\n", x);
va_start (arglist, x);
for (i=0; i<x; i++)
result *= va_arg(arglist, double);
va_end (arglist);
return result;
}
Binary file added Joseph Njoroge/Chapter 15/task3.exe
Binary file not shown.
44 changes: 44 additions & 0 deletions Joseph Njoroge/Chapter 15/task4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
Rewrite the program in Listing 15.3 again. This time, print out all arguments
passed to the AddDouble() function. Does va_arg() fetch each argument in the
same order (that is, from left to right) of the argument list passed to AddDouble()?
*/
#include <stdio.h>
#include <stdarg.h>

double AddDouble(int x, ...);

int main()
{
double d1 = 1.5;
double d2 = 2.5;
double d3 = 3.5;
double d4 = 4.5;
printf("Given an argument: %2.1f\n", d1);
printf("The result returned by AddDouble() is: %2.1f\n\n",
AddDouble(1, d1));
printf("Given arguments: %2.1f and %2.1f\n", d1, d2);
printf("The result returned by AddDouble() is: %2.1f\n\n",
AddDouble(2, d1, d2));
printf("Given arguments: %2.1f, %2.1f and %2.1f\n", d1, d2, d3);
printf("The result returned by AddDouble() is: %2.1f\n\n",
AddDouble(3, d1, d2, d3));
printf("Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n",
d1, d2, d3, d4);
printf("The result returned by AddDouble() is: %2.1f\n",
AddDouble(4, d1, d2, d3, d4));
return 0;
}
/* definition of AddDouble() */
double AddDouble(int x, ...)
{
va_list arglist;
int i;
double result = 0.0;
printf("The number of arguments is: %d\n", x);
va_start (arglist, x);
for (i=0; i<x; i++)
result += va_arg(arglist, double);
va_end (arglist);
return result;
}
Binary file added Joseph Njoroge/Chapter 15/task4.exe
Binary file not shown.
19 changes: 19 additions & 0 deletions Joseph Njoroge/Chapter 16/task1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stdio.h>

int main()
{
char str[] = "My name is Joseph";
int list[] = {1, 2, 3, 4, 5};
char *str_ptr = str;
int *int_ptr = list;

printf("Before the change: %s\n", str);
*(str_ptr + 3) = 'N';
printf("After the change: %s\n", str);

printf("Before the list: %d\n", list[1]);
*(int_ptr + 1) = 78888;
printf("Before the list: %d\n", list[1] );

return 0;
}
Binary file added Joseph Njoroge/Chapter 16/task1.exe
Binary file not shown.
20 changes: 20 additions & 0 deletions Joseph Njoroge/Chapter 16/task2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <stdio.h>

/**
Given a character string, I like C!, write a program to pass the string to a function
that displays the string on the screen.
*/
void stringingify(char *ptr);

int main()
{
char str[] = "I like C!";
char *str_ptr;
str_ptr = str;
stringingify(str_ptr);
}

void stringingify(char *ptr)
{
printf("%s", ptr);
}
Binary file added Joseph Njoroge/Chapter 16/task2.exe
Binary file not shown.
24 changes: 24 additions & 0 deletions Joseph Njoroge/Chapter 16/task3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <stdio.h>

/**
Rewrite the program in exercise 1. This time, change the string of I like C! to I
love C! by moving a pointer that is initialized with the start address of the string
and updating the string with new characters. Then, pass the updated string to the
function to display the content of the string on the screen.
*/

void stringy(char *ptr);
int main()
{
char str[] = "I love C!";
char str2[] = "I like C!";
char *char_ptr;
char_ptr = str;
stringy(char_ptr);
return 0;
}

void stringy(char *ptr)
{
printf("%s", ptr);
}
Binary file added Joseph Njoroge/Chapter 16/task3.exe
Binary file not shown.
13 changes: 13 additions & 0 deletions Joseph Njoroge/Chapter 16/task4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

/**
Given a two-dimensional character array, str, that is initialized as
char str[2][15] = { “You know what,”, “C is powerful.” };
write a program to pass the start address of str to a function that prints out the
content of the character array.
*/

int main()
{

}
13 changes: 10 additions & 3 deletions Joseph Njoroge/Chapter 8/task5.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ characters entered by the user until the character q is accounted. (Hint: Put x!

int main(void)
{
char c = ' ';
c = getchar();
c != 'q' ? 1 : 0;
char c;
printf("Enter a character: ");

for (c = ' '; c != 'A' && c != 'a';)
{
c = getc(stdin);
//printf("%c", c);
putchar(c);
}
printf("\nOut of the loop\n");

return 0;
}
Binary file modified Joseph Njoroge/Chapter 8/task5.exe
Binary file not shown.
1 change: 1 addition & 0 deletions Joseph Njoroge/Chapter 8/tempCodeRunnerFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0;
2 changes: 1 addition & 1 deletion Joseph Njoroge/Chapter 9/task1.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ int main(void)
x = 0xAB78;
y = 0xAB78;

printf("The decimal value of x and y are %d and %u respectively.\n", x, y);
printf("The decimal value of x and y are %d and %d\n respectively.\n", x, y);
return 0;
}
Binary file modified Joseph Njoroge/Chapter 9/task1.exe
Binary file not shown.
12 changes: 12 additions & 0 deletions test1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>


int main()
{
int num;
printf("Enter a single digit that can be divided\nby both 2 and 3:\n");
for (num = 1; (num%2 != 0) || (num%3 != 0);)
num = getchar() - '0';
printf("You got such a number: %d\n", num);
return 0;
}
Binary file added test1.exe
Binary file not shown.