Each book is represented by its authors (a string), its title (a string), its year of publication (an integer), and its price (a float).
typedef struct list LIST;
struct list
{
char *authors;
char *title;
int year;
float price;
LIST *tail;
};
+--------+
| /10 |
+--------+
Start with the allocation and then assign the member values for the book by Kelley and Pohl, with title `C by dissection', published in 2001. Also fill in the price you paid for the book (or what you think it is worth).
LIST *books;
books = (LIST*)calloc(1,sizeof(LIST));
books -> authors = "Kelley and Pohl";
books -> title = "C by dissection";
books -> year = 2001;
books -> price = 23.99;
books -> tail = NULL;
+--------+
| /10 |
+--------+
typedef struct popsize POPSIZE;
struct popsize
{
char *country; /* name of a country */
int population; /* size of its population */
POPSIZE *tail;
};
POPSIZE *insert(POPSIZE *l, char *country, int population)
{
POPSIZE *p;
p = (LIST*)calloc(1,sizeof(LIST));
p -> country = country;
p -> population = population;
p -> tail = l;
return p;
}
+--------+
| /15 |
+--------+
int highest(POPSIZE *l)
{
POPSIZE *p;
int max = 0;
for (p = l; p != NULL; p = p -> tail)
if (p -> population > max)
max = p -> population;
return max;
}
+--------+
| /15 |
+--------+
232.3434531 , 2.2323
0.23 , 8.28232
As you can see, the position of the comma on each line is rather arbitrary.
Let fp be a file pointer variable, x the first, and y the second float variable.
Using these variables, give ONE fscanf command to read x, to skip the comma, and to read the y variable from the file pointed to by fp.
fscanf(fp,"%f%*[^,],%f", &x, &y);
+--------+
| /15 |
+--------+
Assuming that the file is already opened for reading, give the definition below :
int CountLines(FILE *fp)
{
int cnt = 0;
char c;
while ((c = getc(fp)) != EOF)
if (c == '\n') cnt++;
return cnt;
}
+--------+
| /15 |
+--------+
To make sense of this message, execute the following program :
#include
int main()
{
FILE *infile;
char c;
int i,p;
infile = fopen("message","r");
p = 0;
for (i = 0; i < 11; i++)
{
p = (p + 3)%11;
fseek(infile,p,0);
fscanf(infile,"%c",&c);
printf("%c",c);
}
fclose(infile);
return 0;
}
Complete the table with values for p and c below :
------------------------------------------------------------
| i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
============================================================
| p | 3 | 6 | 9 | 1 | 4 | 7 | 10 | 2 | 5 | 8 | 0 |
| c | h | a | p | p | y | e | a | s | t | e | r |
------------------------------------------------------------
+--------+
| /20 |
+--------+