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
32 changes: 32 additions & 0 deletions helloworld/H15_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//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() definition */
void GetDateTime(void) {
time_t now;
int i;
char *str;
printf("Within GetDateTime().\n");
time(&now);
str = asctime (localtime (&now));
printf("Current date and time is: ");
for (i=0; str[i]; i++) printf("%c", str[i]);
}







20 changes: 20 additions & 0 deletions helloworld/H15_E2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//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.

#include <stdio.h>

MultiTwo(int x, int y);

main () {
int i = 24;
int j = 13;
printf("%d multiplied by %d equal %d.\n", i, j, MultiTwo(i,j));
return 0;
}

MultiTwo(int x, int y) {
return x * y;
}

38 changes: 38 additions & 0 deletions helloworld/H15_E3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//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.

#include <stdio.h>
#include <stdarg.h>

double AddDouble(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 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;

}


38 changes: 38 additions & 0 deletions helloworld/H15_E4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//4. 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, ...);
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;
//na ezzel ugyanaz a problémám, mint az előző feladattal. :-(
}


21 changes: 21 additions & 0 deletions helloworld/H16_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//1. Given a character string, I like C!, write a program to pass the string to
//a function that displays the string on the screen.

#include <stdio.h>

void Strprint(char *ch);

main() {
char str[] = "I like C!";
char *ptr_str;
ptr_str = str;
Strprint(ptr_str);
return 0;
}

//function defaul

void Strprint(char *ch) {
printf("%s\n", ch);
}

25 changes: 25 additions & 0 deletions helloworld/H16_E2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//2. 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.#include <stdio.h>
#include <stdio.h>
void Strprint(char *ch);

main() {
char str[] = "I like C!";
char *ptr_str;
ptr_str = str;
*(ptr_str + 3) = 'o';
*(ptr_str + 4) = 'v';
Strprint(ptr_str);
return 0;
}

//function definition

void Strprint(char *ch) {
printf("%s\n", ch);
}



25 changes: 25 additions & 0 deletions helloworld/H16_E3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//3. 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.

#include <stdio.h>

void Strprint(char * ch [][15]);

main() {
char str[2][15] = {"You know what,", "C is powerful."};
char *ptr_str;
ptr_str = str;
Strprint(ptr_str);
Strprint(ptr_str + 15);
return 0;
}

//function defaul

void Strprint(char *ch [][15]) {
printf("%s\n", ch);
//fordításkor nyom egy csomó warningot, amit nem tudok értelmezni, viszont működik.
}

35 changes: 35 additions & 0 deletions helloworld/H16_E4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//4. Rewrite the program in Listing 16.7. This time, the array of pointers is initialized
//with the following strings:
//"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", and
//"Saturday".

#include <stdio.h>
/* function declarations */
void StrPrint1(char **str1, int size);
void StrPrint2(char *str2);

/* main() function */
main() {
char *str[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday\n"};
int i, size = 7;
StrPrint1(str, size);
for (i=0; i<size; i++)
StrPrint2(str[i]);

return 0;
}
/* function definition */
void StrPrint1(char **str1, int size) {
int i;
/* Print all strings in an array of pointers to strings */
for (i=0; i<size; i++)
printf("%s\n", str1[i]);
}
/* function definition */
void StrPrint2(char *str2) {
/* Prints one string at a time */
printf("%s\n", str2);
}


40 changes: 40 additions & 0 deletions helloworld/H17_E1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//1. Write a program to ask the user to enter the total number of bytes he or she wants
//to allocate. Then, initialize the allocated memory with consecutive integers, starting
//from 1. Add all the integers contained by the memory block and print out the final
//result on the screen.

#include <stdio.h>
#include <stdlib.h>

main() {
int SizeofAllmemory;
int *ptr_x;
int sum = 0;
int result;
setbuf(stdout, NULL);
printf("Enter the total number of bytes you want to allocate:");
scanf("%d", &SizeofAllmemory);
ptr_x = malloc (SizeofAllmemory/2 * sizeof(int));
//ptr_x = malloc (SizeofAllmemory); Ez vajon miért nem jó helyette? segmentation faultot ad vissza
if (ptr_x != NULL) {
for (int i = 0; i < SizeofAllmemory/2; i++) {
ptr_x[i] = i + 1;
printf("%d\n", *(ptr_x + i));
}
printf("Az összeg az elején: %d\n", sum);
for (int i = 0; i < SizeofAllmemory/2; i++) {
sum += ptr_x[i];
printf("Az összeg most: %d\n", sum);
}
printf("The sum is %d\n", sum);
result = 0;
}
else {
printf("malloc() function failed.\n");
result = 1;
}
printf("%d", result);
return result;
}


31 changes: 31 additions & 0 deletions helloworld/H17_E2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//2. Write a program that allocates a block of memory space to hold 100 items of the
//float data type by calling the calloc() function. Then, reallocate the block of
//memory in order to hold 50 more items of the float data type.


#include <stdio.h>
#include <stdlib.h>

main() {
float *ptr_x;
int result;
ptr_x = calloc(100, sizeof(float));
if (ptr_x != NULL) {
result = 0;
printf("First allocation is done\n");
}
else {
printf("The first allocation wasn't successful! \n");
result = 1;
}
ptr_x = realloc (ptr_x, 150);
if (ptr_x != NULL) {
result = 0;
printf("The reallocation is done\n");
}
else {
printf("The reallocation wasn't successful! \n");
result = 1;
}
return result;
}
36 changes: 36 additions & 0 deletions helloworld/H17_E3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

//3. Write a program to ask the user to enter the total number of float data. Then use
//the calloc() and malloc() functions to allocate two memory blocks with the
//same size specified by the number, and print out the initial values of the two memory blocks.

#include <stdio.h>
#include <stdlib.h>

main() {
int Nodata;
float *ptr_x1;
float *ptr_x2;
int result;
setbuf(stdout, NULL);
printf("Enter the total number of float data you want to allocate:");
scanf("%d", &Nodata);
ptr_x1 = malloc (Nodata * sizeof(float));
if (ptr_x1 != NULL){
result = 0;
printf("First allocation is done\n, the first value of the block is: %f\n", *ptr_x1);
}
else {
printf("The first allocation wasn't successful! \n");
result = 1;
}
ptr_x2 = calloc (Nodata, sizeof(float));
if (ptr_x2 != NULL) {
result = 0;
printf("Second allocation is done\n, the first value of the block is: %f\n", *ptr_x2);
}
else {
printf("The 2nd allocation wasn't successful! \n");
result = 1;
}
return result;
}
Loading