Final Exam.1

C++ :

-Question 1: length of string

-Correct Solution:

int string_len(char* str){

int i = 0 ;

while (str[i] != 0 ){

    i++ ;

}

return i ; 

}

-Question tip: -

-Question 2:copying a string in a buf

-Correct Solution:

char str_cpy(char* str,char str_copy[20])

{ int i = 0 ;

while(str[i] != 0 )

    str_copy[i] = str[i] ;

str_copy[i] = str[i] ;

return str_copy[20] ; 

}

-Question tip: ِ یک حافظه تخصیص یافته است و جمله اخر نیز باید در ان نیز قرار گیرد str_copy توجه به ابن نکته که str_copy[i] = str[i]

-Question 3:counting a character in a string

-Correct Solution:

int char_count(char * str , char a) {

int i =0 ;

int  s =0 ;  

while (str[i] != 0 )

    if (str[i] == a )

        s++ ;
     

return s ;

}

-Question tip: -

-Question 4: counting longest sequence of a character

-Correct Solution:

int longest_char_sequence(char a[], char b)

{

int max =0 ;

int n = string_len(a);

int i =0 ;

while (i< n )

{

        if (a[i]==b)
        {
            int k =0 ;
            while(a[i]==b)
            {
                i+=1 ;
                k+=1;


            }

            if (k>max)
                max = k ;
            i-=1 ;

        }
        i+=1 ;

}
return max ;

}

-Question tip:

-Question 5: counting a pattern of characorts in a stirng

-Correct Solution:

-Question tip:

-Question 6:applying a function

-Correct Solution:

typedef int (*_ii2i_fp)(int ,int );

int * apply (int len , int * l1 , int* l2 , _ii2i_fp def,int*ans )

{

for (int i =0 ; i< len ; i++)

{

    ans [i]= def (l1[i] , l2[i]);

}

return ans ; 

}

-Question tip: نحوه استفاده از یک تابع تعریف شده

-Question 7:changing a struct

-Correct Solution:

typedef struct student

{

char id[9];

char name[30];

}student ;

void change_student_id (student* stu ,char * new_id )

{

str_cpy(new_id , stu -> id );

}

void change_student_name(student* stu , char* new_name )

{

str_cpy(new_name , stu ->name  );

}

-Question tip: توجه به تغییر رشته های حرفی داخل استراکت به صورت جمله به جمله و استفاده از تابع str_cpyدر این سوال

-Question 8: converting a struct to string

-Correct Solution:

char * student_tostring(student* s )

{

char* buf = ( char * )malloc (40 * sizeof(char ));

char* buf1 = buf   ;

str_cpy(s->id , buf );

buf += string_len(s->name);

*buf += '\0' ; 

return buf1 ;

}

-Question tip:در نظر گرفتن یک حافظه مشخص و برابر با استراکت با دستور malloc

-Question 9:creating a struct

-Correct Solution:

student * create_student(char id2[9] , char name2[30])

{

student* stud = (student*)malloc(sizeof(student));

str_cpy(name2 , stud->name);

str_cpy(id2 , stud-> id );

return stud ; 

}

-Question tip:چگونگی تعریف رشته های حرفی داخل استراکت بدون تعریف جداگانه و مقدار دهی مستقیم

-Question 10:memory

-Correct Solution:

char* get_std_ptr1(student * ps)

{

char * new_ptr = ((char*)ps )+ 5 ;

return new_ptr ;

}

char* get_std_ptr2(student* ps )

{

char* new_ptr = ((char* )ps)+ 12;

return new_ptr ; 

}

-Question tip:توانایی استفاده از دستور -exec x/32bx <address>


2019

Hackathon

1 minute read

هدف پروژه : -در این ترم در درس مبانی برنامه سازی کامپیوتر به کمک استاد گرامی و تی ای محترم مشغول به ساختن یک وبسایت با هدف دلخواه شدیم که ایده ی من برای ...

Back to top ↑

2016

Course Website

less than 1 minute read

شرح وبسایت استاد محمدی: سیلابس درس سیگنال ها و سیستم ها در بخش مربوطه قرار داده شده است . ایمیل و مشخصات استاد محترم قرار داده شده است . -در بخش پایان...

ِDYI

less than 1 minute read

استفاده مفید از یک هارد بلااستفاده

Back to top ↑