Answers

Questions
19/01/05
Question 4
---------Straightforward implementation, but note that it assumes more about
strcpy()
than ANSI standard allows (ANSI doesn't allow overlapping strings).
But you
studied a specific strcpy() implementation [while (*dst++ =
*src++);].
void trim(char *str)
{
int i;
1
for (i = 0; str[i]; i++)
if (str[i] == ' ')
if (!i || !str[i+1] || str[i+1] == ' ') {
strcpy(str + i, str + i + 1);
--i;
}
}
This is a function that deletes spaces (' ') from a string
Can you tell me what is the meaning of !i and why is it equal to
str[0]!=NULL
//inserts into a sorted list while keeping it sorted
//(ascending by key)
item * insert(item *head, item *newNode)
{
item *currItem;
if (!head)
return newNode;
2.
//check if newNode's key is smaller than all keys
//and should be first
if (newNode->key < head->key)
{
newNode->next = head;
return newNode;
}
currItem = head;
while (currItem->next && newNode->key > currItem-
>next->key)
currItem = currItem->next;
//put newNode between currItem and currItem->next
//(if currItem is last then currItem->next == NULL)
newNode->next = currItem->next;
currItem->next = newNode;
return head;
}
Its part of your lesson, If we have a linked list with the numbers 2,5,7,9 and we want
to add the number 6 to the list. From what I understood the curritem will point to 7,
and also the new node(i.e. number 6) I didn’t understand how did you make the struct
that contains number 5 to point to the new node?
//adds a new item to the end of the list
void add_last(LinkedList * list, item* newItem)
{
if (!list->head)
{
list->head = list->tail = newItem;
return;
3.
}
newItem->prev = list->tail;
list->tail->next = newItem;
list->tail = newItem;
}
I also don’t understand that function….please explain
4.
What function can I use in order to convert the string's characters to the integer
values? And the opposite way?
‫‪I was wondering what is the function to sort struct‬‬‫) ‪5. lists ? (by its value‬‬
‫‪I didn't see any example for it in any of the books.‬‬
‫האם ניתן לקבל דוגמא לדפי התשובות הניתנים במבחן ‪ -‬זאת ע"מ‬
‫שנוכל להתכונן בצורה טובה יותר‬
‫‪6.‬‬
‫בשאלות מסוג "כתוב פונק" ‪ -‬האם מותר להשתמש בפונק' עזר בנוסף לפונק' הנדרשת?‬
‫(אם לא מצויין אחרת)‪.‬‬
‫‪7.‬‬
‫צירפתי פה את המבחן של שנה שעברה ואת הפתרון שלה‪ .‬רציתי לדעת אם תוכל לענות לי‬
‫לגבי התשובה לשאלה ‪ 6‬בנושא הקבצים‪.‬‬
‫לא הצלחתי להבין מה התפקיד של ‪ f1‬ו ‪ f2‬בתוכנית‪.‬הוא מבצע שם השמה מוזרה בתחילת‬
‫הלולאה‪..‬‬
‫אודה לך אם תוכל לחזור אליי‪....‬‬
‫‪8.‬‬
‫שאלה ‪ 25( 6‬נקודות)‬
‫נתונים שני קבצים של ציונים של סטודנטים ממחלקה מסוימת‪:‬‬
‫‪‬‬
‫הקובץ הראשון מכיל פרטי ציוני המבחן‪ .‬כל שורה שמכילה את הפרטים הבאים‪:‬‬
‫שם הסטודנט – עד ‪ 20‬תווים‬
‫מספר ת‪.‬ז‪ 9 – .‬ספרות‬
‫ציון מבחן – ‪ 3‬ספורות‬
‫‪‬‬
‫הקובץ השני מכיל פרטי ציוני העבודות‪ .‬כל שורה שמכילה את הפרטים הבאים‪:‬‬
‫מספר ת‪.‬ז‪ 9 – .‬ספרות‬
‫ציון עבודה ‪ 3 – 1‬ספורות‬
‫ציון עבודה ‪ 3 – 2‬ספורות‬
‫ציון עבודה ‪ 3 – 3‬ספורות‬
‫שני קבצים הנ"ל ממוינים לפי מספר ת‪.‬ז בסדר עולה‪.‬‬
‫כתוב פונקציה ) ‪ final( . .‬שמקבלת כארגומנטים שני פוינטרים על הקבצים הנ"ל ומייצרת קובץ שלישי‪.‬‬
‫הקובץ השלישי מכיל ריכוז הציונים והציון הסופי של כל סטודנט‪ .‬כל שורה בקובץ מכילה את הפרטים‬
‫הבאים‪:‬‬
‫שם הסטודנט – עד ‪ 20‬תווים‬
‫מספר ת‪.‬ז‪ 9 – .‬ספרות‬
‫ציון עבודה ‪ 3 – 1‬ספורות‬
‫ציון עבודה ‪ 3 – 2‬ספורות‬
‫ציון עבודה ‪ 3 – 3‬ספורות‬
‫ציון מבחן – ‪ 3‬ספורות‬
‫ציון סופי ‪ 3 -‬ספרות (לפי ‪ 30%‬עבודות ‪ 70%‬בחינה)‬
‫שימו לב‪ :‬אם סטודנט לא ניגש לבחינה לא יופיעו ‪ 2‬הציונים האחרונים‪ ,‬אבל אם נבחן ולא הגיש עבודות ציוני‬
‫העבודות הם אפס‪.‬‬
‫הפונקציה מקבלת את השם של הקובץ השלישי כארגומנט שלישי‪.‬‬
‫הגבלה‪ :‬אין לקרוא קובץ יותר מפעם אחת‪.‬‬
‫אין להעתיק קובץ למבנה נתונים אחר (מערך‪ ,‬רשימה משורשרת‪ ,‬עץ‪)...‬‬
‫פתרון‬
‫{ )‪void final(FILE* exam, FILE* ass, char* summery_filename‬‬
‫;‪FILE *summery‬‬
‫;]‪char name_ex[21],name_ass[21‬‬
‫;‪long id_exam, id_ass‬‬
‫;‪int flag1, flag2, f1, f2‬‬
‫;‪int exam_gr, final_gr‬‬
int gr1, gr2, gr3;
summery = fopen(summery_filename,"w");
flag1 = fscanf(exam, "%[^0-9]%9d%3d%*c", name_ex, &id_exam,
&exam_gr);
flag2 = fscanf(ass,"%[^09]%9d%3d%3d%3d%*c",name_ass,&id_ass,&gr1,&gr2,&gr3);
while (flag1 != EOF || flag2 != EOF) {
f1 = (flag1 != EOF);
f2 = (flag2 != EOF);
if (f1 && f2 && id_exam == id_ass) {
final_gr = (7*exam_gr+gr1+gr2+gr3)/10;
fprintf(summery,
"%s%9d%3d%3d%3d%3d%3d\n",name_ex,
id_exam, gr1, gr2, gr3, exam_gr, final_gr);
flag1 = fscanf(exam, "[^0-9]%9d%3d%*c", name_ex,
&id_exam, &exam_gr);
flag2 = fscanf(ass, "[^0-9]%9d%3d%3d%3d%*c",name_ass,
&id_ass, &gr1, &gr2, &gr3);
}
else
if ((f1 && f2 && id_exam < id_ass) || !f2) {
final_gr = 7*exam_gr/10;
fprintf(summery, "%s%9d%3d%3d%3d%3d%3d\n",
name_ex, id_exam, 0, 0, 0, exam_gr, final_gr);
flag1 = fscanf(exam, "%20s%9d%3d%*c", name_ex,
&id_exam, &exam_gr);
}
else
if ((f1 && f2 && id_exam > id_ass) || !f1) {
fprintf(summery,"%s%9d%3d%3d%3d\n",name_ass,id_ass,gr1,gr2,gr3);
flag2 = fscanf(ass, "%[^09]%9d%3d%3d%3d%*c",name_ass,
&id_ass, &gr1, &gr2, &gr3);
}
‫}‬
‫;)‪fclose(summery‬‬
‫}‬
‫‪when giving a question like this‬‬
‫שאלה ‪ 18( 5‬נקודות)‬
‫נתון קובץ נתוני הסטודנטים של הקורס שלנו‪ ,‬כל שורה בקובץ מכילה את הפרטים הבאים‪:‬‬
‫‪-‬‬
‫שם הסטודנט ‪20 -‬תווים‬
‫‪-‬‬
‫כתובת‬
‫‪20 -‬תווים‬
‫‪-‬‬
‫מספר ת‪.‬ז‪.‬‬
‫‪9 -‬ספרות‬
‫‪-‬‬
‫קוד מחלקה‬
‫‪3 -‬ספרות‬
‫‪-‬‬
‫קוד תרגול‬
‫‪- 2‬ספרות (קוד בין ‪ 11‬ל‪)99-‬‬
‫הקובץ מסודר לפי מספר ת‪.‬ז‪ .‬בסדר עולה‪.‬‬
‫כתוב פונקציה ‪ new_files‬שמקבלת כפרמטר מצביע לקובץ הנ"ל ויוצרת מספר קבצים חדשים כמספר‬
‫התרגולים בפועל בקורס‪ .‬המספר הזה אינו ידוע מראש (ומשתנה במהלך הרישום‪).‬‬
‫‪-‬‬
‫שם כל קובץ הוא ”‪ “tirgul_NN.dat‬כאשר ‪ NN‬הוא קוד התרגול‪ .‬כל שורה בקובץ מכילה‪:‬‬
‫‪-‬‬
‫שם הסטודנט ‪20 -‬תווים‬
‫‪-‬‬
‫מספר ת‪.‬ז‪.‬‬
‫‪9 -‬ספרות‬
‫‪-‬‬
‫קוד מחלקה‬
‫‪3 -‬ספרות‬
‫הקבצים מסודרים לפי מספר ת‪.‬ז‪ .‬בסדר עולה‪.‬‬
‫לדוגמא אם בקורס נפתחו שלוש קבוצות תרגול ‪ 11 ,43 ,27‬הפונקציה יוצרת בדיוק שלושה קבצים‬
‫שהם‪: tirgul_27.dat, tirgul_43.dat, tirgul_11.dat .‬‬
‫הגבלה‪ :‬אין לקרוא את קובץ הנתונים יותר מפעם אחת ‪.‬‬
‫אין להעתיק קובץ למבנה נתונים אחר (מערך‪ ,‬רשימה משורשרת‪ ,‬עץ)‪...‬‬
‫‪can i make an array just for course code‬‬
‫‪9.‬‬
...‫שאלה נוספת לי אליך‬
word. ‫ ו‬line ‫ המקבלת פרמטרים שתי מחרוזות‬last ‫"כתוב פונקצייה רקורסיבית בשם‬
‫ הפונקציה מחזירה את‬word.‫ את ההופעה האחרונה של‬line ‫הפונקצייה מחפשת במחרוזת‬
10.
‫ אם היא לא מופיעה בה בכלל אזי‬line. ‫ בתוך‬word ‫המיקום של תחילת התת מחרוזת‬
."‫הפונקצייה מחזירה מינוס אחד‬
....‫אם תוכל לענות לי על שאלה זו אשמח מאוד‬
.‫תודה‬
need to get something cleared out. What is the difference
between:
1. char A[10];
2. char *A=(char*)malloc(sizeof(char)*10);
11.
The reason I'm asking is because I get an error message
when I'm using realloc later on in my program:
A = (char*)realloc(A,sizeof(char)*20);
I only get the error message in option 1 but in option 2
I don't get the error message.
This is my program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char A[10];
sprintf(A,"0123456789");
A=(char*)realloc(A,sizeof(char)*20);
sprintf(A,"abcdegf");
puts(A);
}
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.