מבוא לתכנות ולמדעי המחשב בשפת C/שיעורי חזרה

לדף הקורס

שיעור חזרה ראשון

עריכה
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int add(int a, int b) {
    if(b==0)
	return a; 
    return add(a+1,b-1); 
}

int pow1(int a, int n) {
    if(n==0)
	return 1; 
    return a*pow1(a,n-1); 
}

int pow2(int a, int n) {
    if(n==1)
	return a;
    int t = pow2(a,n/2); 
    if(n%2 == 0) 
	return t*t; 
    return t*t*a; 
}

int find(char c, char *str, int from, int to) {
    int i; 
    for(i=from; i<=to; ++i)
	if(str[i] == c)
	    return i; 
    return -1; 
}

int evaluateRec(char *str, int from, int to) {
    int i = find('+',str,from,to); 
    if(i != -1) 
	return evaluateRec(str,from,i-1)+evaluateRec(str,i+1,to); 
    i = find('X',str,from,to); 
    if(i != -1) 
	return evaluateRec(str,from,i-1) * evaluateRec(str,i+1,to); 
    return atoi(str+from); 
}

int evaluate(char* str) {
    return evaluateRec(str,0,strlen(str)-1); 
}

int main() {
    printf("%d\n",add(3,4)); 
    printf("%d\n",pow1(2,5)); 
    printf("%d\n",pow2(2,5)); 
    printf("%d\n",evaluate("445X23+23X21")); 
    return 0; 
}

שיעור חזרה שני

עריכה

isPalindrome

עריכה
int isPalindrome(char *str) {
    int n=0; 
    while(str[n] != '\0')
	++n; 
    int i; 
    for(i=0; i<n/2; ++i)
	if(str[i] != str[n-1-i])
	    return 0;
    return 1; 
}

int main() {
    char *array[] = {"ere","wewew","neveroddoreven","gfgf","neveroddorevenw"}; 
    int n = sizeof(array)/sizeof(char*); 
    int i; 
    for(i=0; i<n; ++i) {
	if(isPalindrome(array[i]))
	    printf("%s is a palindrom\n",array[i]); 
	else 
	    printf("%s is *not* a palindrom\n",array[i]); 
    }

    return 0;
}
int b4max(int *array, int n) {
    int i,max,b4;
    b4 = max = array[0]; 
    for(i=1; i<n; ++i)
	if(array[i] > max) {
	    b4 = max; 
	    max = array[i]; 
	} else
	    if(array[i] > b4)
		b4 = array[i]; 
    return b4; 
}

int main() {
    int array[] = {3,9,1,2,3,7,11,45,12,1}; 
    int n = sizeof(array)/sizeof(int); 
    printf("%d \n",b4max(array,n)); 
    return 0;
}

draw memory.. what is the output

עריכה
int main() {
    int array[10]; 
    int i; 
    for(i=0; i<10; ++i) 
	array[i] = i+1; 
    
    int *p1 = array, *p2 = array+9; 
    int **pp = &p1; 

    for(i=0; i<5; ++i) {
	printf("%d \n",**pp); 
	++p1; 
	--p2; 
	if(pp == &p1)
	    pp = &p2; 
	else
	    pp = &p1; 
    }
    return 0;
}

draw memory.. what is the output

עריכה
struct A {
    int data; 
}; 

struct B {
    struct A a; 
    struct A *ptr; 
}; 

int main() {
    struct B array[10]; 
    int i; 
    for(i=0; i<9; ++i) {
	array[i].a.data = i; 
	array[i].ptr = & (array[i+1].a); 
    }
    array[9].a.data = 9; 

    for(i=0; i<9; ++i) 
	printf("%d \n",(array[i].ptr)->data); 

    return 0;
}
#include <stdio.h>
#include <stdlib.h> 

void swap(int *p1, int *p2) {
    int tmp = *p1; 
    *p1 = *p2; 
    *p2 = tmp; 
}

int main() {

    int array[10]; 
    int i; 
    for(i=0; i<10; ++i)
	array[i] = i+1; 
	
    for(i=9; i>=0; --i) {
	int j = rand()%(i+1); 
	printf("%d \n",array[j]);
	swap(array+j,array+i); 
    }

    return 0; 
}