CS 240 Exam 1

0.0(0)
studied byStudied by 0 people
full-widthCall with Kai
GameKnowt Play
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/53

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

54 Terms

1
New cards

Write the command to compile a single C file named "hello.c" into an object file called "hello.o".

$ gcc -c hello.c

2
New cards

Write the command to link two object files named "hello.o" and "goodbye.o" into the executable called "application".

$ gcc -o application hello.o goodbye.o

3
New cards

Can you "run" an object file if it contains the "main()" function?

No

4
New cards

Can you "run" an executable that contains a single function called "main()"?

Yes

5
New cards

Can you "run" an executable that does not contain a function called "main()"?

No

6
New cards

What does the "-Wall" flag do?

Enable ALL warnings

7
New cards

What does the "-g" flag do?

include debug symbols

8
New cards

What does the "-ansi" flag do?

Adhere to the ANSI standard

9
New cards

What does the "-c" flag do?

compile file into object code

10
New cards

What does the "-o" flag do?

output to file

11
New cards

What does the "-Werror" flag do?

Turn warnings into errors

12
New cards

What does the "-O" flag do?

Optimize the output file

13
New cards

What does the "-std = X" flag do?

Adhere to some standard X

14
New cards

Given the following FILE pointer variable definition, write the code that will open a file named "hello.txt" for read-only access and print a message of your choice if there was an error in doing so.

FILE *my_file = 0;

my_file = fopen("hello.txt", "r");

if (my_file == NULL) {

printf("File cannot be opened!");

}

15
New cards

Write code that will, without opening any file, check if a file named "hello.txt" can be opened for read access. Put the code inside the 'if' predicate:

if ( ) {

/ Yes, we can open the file... /

}

if (access ("hello.txt", R_OK) == 0)

16
New cards

What does the "R_OK" flag for access do?

Check for the read access

17
New cards

What does the "W_OK" flag for access do?

check for the write access

18
New cards

What does the "F_OK" flag for access do?

Check for existence

19
New cards

What does the "r" flag for fopen do?

open the file for reading only and must already exist

20
New cards

What does the "w" flag for fopen do?

creates a file for writing or if it already exists overwrite it.

21
New cards

What does the "a" flag for fopen do?

appends to the end of the file and if it doesn't exist it will be created

22
New cards

Write code that will, without opening any file, check if a file named "hello.txt" can be opened for write access. Put the code inside the 'if' predicate:

if ( ) {

/ Yes, we can open the file... /

}

if (access ("hello.txt", W_OK) == 0)

23
New cards

Write a function called read_and_print() that will do the following:

. Open a text file called "hello.txt" for read-only access.

. Read a word that is terminated by a newline from the file into the character array called "my_string".

. Read an integer terminated by a newline into the int variable called "my_int".

. Print the string and the integer value.

. Return the my_int value.

- If the file cannot be opened for reading, return -1.

- If an error occurs while reading from the file, return -1.

int read_and_print() {

FILE *fp = NULL;

char my_string[10] = {'\0'};

int my_int = 0;

int check_scanf = 0;

fp = fopen("hello.txt", "r");

if (fp == NULL) {

return -1;

}

check_scanf = fscanf(fp, "%[A-Za-z]", my_string);

if (check_scanf != 1) {

return -1;

}

check_scanf = fscanf(fp, "%d", &my_int);

if (check_scanf != 1) {

return -1;

}

printf("%s %d\n", my_string, my_int);

return my_int;

}

24
New cards

Write a function named print_reverse that will open a text file named "hello.txt" and print each character in the file in reverse. i.e. print the first character last and the last character first. The function should return the number of characters in the file. Upon any error, return -1. HINT: Use fseek() a lot to do this.

int print_reverse() {

FILE *fp = NULL;

char my_char = '\0'

int location = 0;

int check_scanf = 0;

fp = fopen("hello.txt", "r");

if (fp == NULL) {

return -1;

}

seek(fp, 0, SEEK_END);

location = ftell(fp);

for (int i = 0; i < location; i++) {

check_scanf = fscanf(fp, "%c", &my_char);

if (check_scanf != 1) {

return -1;

}

printf("%c", my_char);

fseek(fp, -1, SEEK_CUR);

}

return location;

25
New cards

what does ftell do?

it is used to find out where we are in the file

26
New cards

what does fseek do?

it is used to "go somewhere" in the file

27
New cards

What does "SEEK_SET" do?

new offset will be relative to the beginning of the file

28
New cards

What does "SEEK_CUR" do?

new offset will be relative to the current position

29
New cards

What does "SEEK_END" do?

new offset will be relative to the end of the file

30
New cards

Write a function that defines a structure, initializes it, writes it to a file called "struct.out", closes the file, re-opens the file for read-only access, reads a single structure into a new struct variable and then closes the file. Print the structure contents to the screen. On any error, return -1. Otherwise return 0.

typedef struct {

int x;

int y;

} vector_t;

int write_and_read_struct() {

vector_t vector = {0, 0};

vector_t vector2 = {0, 0};

FILE *fp = NULL;

int check = 0;

fp = fopen("struct.out", "wb");

if (fp == NULL) {

return -1;

}

check = fwrite(&vector, sizeof(vector), 1, fp);

if (check != 1) {

return -1;

}

fclose(fp);

fp = NULL;

fp = fopen("struct.out", "rb");

if (fp == NULL) {

return -1;

}

check = fread(&vector2, sizeof(vector2), 1, fp);

if (check != 1) {

return -1;

}

printf("%d %d\n", vector2.x, vector2.y);

return 0;

}

31
New cards

What is the prototype for fread & fwrite?

(void ptr, int size, int num, FILE fp)

32
New cards

Declare a type called "my_array_t" that is an array of 15 floats.

typedef float my_array_t[15];

33
New cards

Declare a type called "struct_arr_t" that is an array of 10 structs of the format

struct str {

int x;

int y;

};

typedef struct str struct_arr_t[10];

34
New cards

Define a variable called my_str_arr of type struct_arr_type.

struct_arr_type my_str_arr;

35
New cards

Can two elements within a structure have the same name?

No

36
New cards

Can you initialize a structure like this?

struct my_str {

int x;

float y;

} mine = { 0, 0.0 };

Yes

37
New cards

Can you initialize a structure like this?

struct my_str {

int x;

float y;

};

void my_func(int n) {

my_str mine = { n, 0.0 };

}

Yes

38
New cards

Declare a structure that contains an integer element named i, a floating point element named f, and an array of 20 characters named str (in that order). Name it anything you want.

struct yayyy {

int i;

float f;

char str[20];

};

39
New cards

Define a variable called "my_new_struct" of the type in the previous question.

typedef struct yayyy my_new_struct;

40
New cards

Define a variable called "my_array_of_structs" that is an array of 40 structures of the type in the prior two questions.

typedef struct yayyy my_array_of_structs[40];

41
New cards

Define a function called bigger_rectangle() that will accept one argument of the structure type rectangle (declared below) and will multiply the width dimension by 1.5, the height dimension by 2.5 and the length dimension by 3. The function should return the new structure.

Define a temporary local variable if you want to.

struct rectangle {

float height;

float width;

float length;

};

struct rectangle bigger_rectangle(struct rectangle rectangle_t) {

rectangle_t.height = rectangle_t.height * 1.5;

rectangle_t.width = rectangle_t.width * 2.5;

rectangle_t.length = rectangle_t.length * 3;

return rectangle_t;

}

42
New cards

Write a function named sum_rectangles that will open a binary file named "rect.in" for reading and read the binary images of rectangle structures from it. For each rectangle structure, add it's elements to those of the first structure read. e.g. sum the height fields of all the structures, sum the width fields of all the structures, etc... Return a structure from sum_rectangles where each element represents the sum of all structures read from the file. i.e. the height field should be the sum of all of the height fields of each of the structures. On any file error, return the structure { -1.0, -1.0, -1.0 }.

struct rectangle sum_rectangle() {

FILE *fp = NULL;

struct rectangle err = {-1.0, -1.0, -1.0};

struct rectangle in_rec = {0.0, 0.0, 0.0};

struct rectangle sum_rec = {0.0, 0.0, 0.0};

int read_check = 0;

fp = fopen("rect.in", "rb");

if (fp == NULL) {

return err;

}

while (read_check != EOF) {

read_check = fread(&in_rec, sizeof(in_rec), 1, fp);

if (read_check != 1) {

return err;

}

sum_rec.height += in_rec.height;

sum_rec.width += in_rec.width;

sum_rec.length += in_rec.length;

}

fclose(fp);

fp = NULL;

return sum_rec;

}

43
New cards

Under what circumstances would you place an assert() into your code?

if there is any way of detecting and recovering from an error or if you cannot recover.

44
New cards

What will be the result of the following code:

int my_func() {

int count = 0;

int sum = 0;

for (count = 0; count < 100; count++) {

assert(sum > 0);

sum = sum + count;

}

return sum;

}

&

What might you do the previous code to make it do a "better" job?

The code will abort and return an assertion error at the line it occurred

To make the assertion condition greater than or equal to 0.

45
New cards

Write a function called do_compare() that will prompt the user for two strings of maximum length 100. It should compare them and print one of the following message: The strings are equal. The first string comes before the second. The second string comes before the first. The function should always return zero.

int do_compare() {

char str1[100] = {'\0'};

char str2[100] = {'\0'};

printf("Enter the first string with a maximum of 100

characters");

scanf("%[a-zA-Z ,.;']", str1);

printf("Enter the second string with a maximum of

100 characters");

scanf("%[A-Za-z .,;']", str2);

if (strcmp(str1, str2) == 0) {

printf("The strings are equal");

}

else if (strcmp(str1, str2) < 0) {

printf("The first string comes before the second");

}

else {

printf("The second string comes before the first");

}

return 0;

}

46
New cards

What is the different between initialization of a variable and assignment to a variable.

The initialization gives a variable an initial value upon its creating while an assignment gives a variable a value at some point after its creation.

47
New cards

What is the difference between a declaration and a definition.

A definition allocates storage for a variable while a declaration announces the properties of a variable.

48
New cards

What is the difference between a global variable and a local variable.

A global variable can be access by all functions globally while a local variable can only be access by the scope of its definition.

49
New cards

For the following questions, assume that the size of an 'int' is 4 bytes, the size of a 'char' is one byte, the size of a 'float' is 4 bytes and the size of a 'double' is 8 bytes. Write the size of the following expressions:

struct my_coord {

int x;

int y;

double altitude;

};

struct my_line {

struct my_coord first;

struct my_coord second;

char name[10];

};

struct my_coord var;

struct my_coord array[3];

struct my_line one_line;

struct my_line two_lines[2];

sizeof(struct my_coord) = _______

sizeof(var). = _______

sizeof(array[1]) = _______

sizeof(array[2]) = _______

sizeof(array) = _______

sizeof(struct my_line) = _______

sizeof(two_lines) = _______

sizeof(one_line) = _______

16, 16, 16, 16, 48, 42, 84, 42

50
New cards

Consider the following variable definitions:

int x = 2;

int arr[10] = {4, 5, 6, 7, 1, 2, 3, 0, 8, 9};

int *p;

And assume that p is initialized to point to one of the integers in arr. Which of the following statements are legitimate? Why or why not?

1) p = arr; arr = p; p = &arr[2]; p = arr[x]; p = &arr[x]; arr[x] = p; 2) arr[p] = x; &arr[x] = p; p = &arr; x = arr; x = arr + x; 3) p = arr + x; arr = p + x; x = &(arr+x); p++; x = --p; x = p++; 4) x = (p)++; arr++; x = p - arr; x = (p>arr); arr[p]=p; p++ = x; 5) p = p + 1; arr = arr + 1;

1) T, T, T, F, T, F 2) F, T, F, T, F 3) T, F, F, F, T, F, T 4) T, F, T, T, T, T 5) T, F

51
New cards

int fprinf(

(FILE file pointer, const char format, ...);

fprintf() returns the number of characters output.

52
New cards

int fscanf

(FILE file pointer, const char format, ...);

fscanf() returns the number of items parsed.

53
New cards

int feof

(FILE *file pointer);

feof() returns 1 if the file pointer has reached the end of file, otherwise 0.

54
New cards

double log10

(double x)

returns the base 10 logarithm of x