Final Exam Review

ECE 15 Fall 2013
UCSD
Grant R. Vousden-Dishington
char w[] = "final";
w[3] = '\0';
printf("w = %s\n", w);

char *words = "tropical";
int i;
for (i = 3; i >= 0; i--)
printf("%c", i[words]);
w = finport

#include <stdio.h>

int g = 0;

void my_add(int x){ g += x; }

void my_sub(int x){ g -= x; }

int main()
{
    int x = 1;
    switch (x++)
    {
        case 0: my_add(5);
        case 1: my_add(4);
        case 2: my_sub(3);
        case 3: my_add(2);
        default: my_sub(1);
    }
    printf("g = %d x = %d\n", g, x);
    return 0;
}
 g = 2 x = 2

#include <stdio.h>

int my_func(int i)
{
    static int s;
    return i + s++;
}

int main()
{
    int i, s;
    for (i = 0; (s = my_func(i)) < 3; i++);
    printf("i = %d s = %d\n", i, s);
    return 0;
}
 i = 2 s = 4

int j = 0, n = 9;

if (j++ && --n*1) {
    switch (++j) {
        case 0: n -= 4*j++;
        case 1: n -= 3*j++;
        case 2: n -= 2*j++;
        default: n -= 1*j++;
    }
} else {
    switch (++j) {
        case 0: n += 3*j++;
        case 1: n += 2*j++;
        case 2: n += 1*j++;
        default: n += 4*j++;
    }
}
printf("j = %d   n = %d\n", j, n);
 j = 4   n = 23

Assign the following type of pattern to a multidimensional array called  my_arr,  and print it with the following code. ANY SIZE!

0 0 0 00 0 0 10 0 1 10 1 1 1
#include <stdio.h>
#define SIZE 4
int main()
{
    int i, j;
/////////////////////////////
//YOUR CODE HERE
/////////////////////////////
    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
           printf("%d ", my_arr[i][j]);
        printf("\n");
    }
    return 0;
}
#include <stdio.h>
#define SIZE 4
int main()
{
    int i, j;
    int my_arr[SIZE][SIZE];
    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++)
            my_arr[i][j] = (i + j >= SIZE) ? 1 : 0;

    for (i = 0; i < SIZE; i++)
    {
        for (j = 0; j < SIZE; j++)
            printf("%d ", my_arr[i][j]);
        printf("\n");
    }
    return 0;
}
Write a program that scans input from a file and writes its output to another file, with names specified on the command line. The input file contains a list of (decimal) scores from 0 to N, inclusive, where N can be any integer. Assume N is defined. The output file should give a 'histogram' of the scores - print N lines, starting with the score and followed by a number of asterisks (*) equal to the number of times that score appeared in the input file. Round scores that are not integers to nearest value.
Input file  (N=5)                            Output file
1                                       0: *
4                                       1: **
2                                       2: ***
3                                       3: ***
4                                       4: ****
4                                       5: 
2
3
4
1
2
3
0
Execution
 $ ./a.out <INPUT_FILE_NAME> <OUTPUT_FILE_NAME>

#include <stdio.h>
#define N 5
int main(int argc, char *argv[])
{
    int i,j, hist[N+1] = {0};
    double score;
    FILE *file_ptr = fopen(argv[1],"r");

    while(fscanf(file_ptr,"%lf",&score) != EOF)
        hist[(int) (score+.5)]++;

    fclose(file_ptr);

    file_ptr = fopen(argv[2],"w");
    for(i = 0; i <= N; i++)
    {
        fprintf(file_ptr,"%d: ",i);
        for(j = 0; j < hist[i]; j++)
            fprintf(file_ptr,"*");
        fprintf(file_ptr,"\n");
    }

    fclose(file_ptr);
}
Write the following functions. Assume DECK_SIZE is defined at the top of the file and the function int generate_random() can be used to return a random integer between 0 and DECK_SIZE.
void fill_deck(int card_deck[]) 
/* fill_deck() puts the numbers 1 through DECK_SIZE into the card_deck array.  
 * These values are inserted into the array in increasing order.  Be careful  
 * not to exceed the allowed array index values!    
 *  
 * Use array notation when implementing this function. You can assume that   
 * card_deck is of size DECK_SIZE. */
void swap(int *x, int *y)
/* swap() swaps the integer values pointed to by x and y. */
void shuffle_deck(int card_deck[], int cards_in_pile, int num_swaps)  
/* shuffle_deck() swaps two randomly chosen cards in the pile, and does this  
 * num_swaps times.  To choose the random cards, generate_random() is  
 * called.  To swap the cards, swap() is called.  Assume there are  
 * cards_in_pile cards in the pile to shuffle.    
 *  
 * Make sure that ALL  
 * valid indices of the array are candidates for swapping, where the valid  
 * indices are dependent on the number of cards in the pile, even though  
 * card_deck has a length of DECK_SIZE.  */  

#define DECK_SIZE 52

void fill_deck(int card_deck[])
{
    int i;
    for(i = 0; i < DECK_SIZE; i++)
        card_deck[i] = i+1;
}

void swap(int *x, int *y)
{
    int temp = *x;
    *x = *y;
    *y = temp;
}

void shuffle_deck(int card_deck[], int cards_in_pile, int num_swaps)
{
    int i, card1, card2;
    for(i = 0; i < num_swaps; i++)
    {    
        while((card1 = generate_random()) >= cards_in_pile);
        while((card2 = generate_random()) >= cards_in_pile);
        swap(&card_deck[card1], &card_deck[card2]);
    }
}

HOW TO DYNAMICALLY ALLOCATE MEMORY

Pick whatever kind of data you need. In the example below, we use doubles. After you call malloc(), use your variable, essentially, the same way you would use an array.
#include <stdio.h>
#define MAX 5
int main()
{
    // 1. Declare your pointer
    double *myData;

    // 2a. Use sizeof to get size of your datatype
    // 2b. Multiply by how many you need
    // 2c. Call malloc, pass the above value
    // 2d. Cast to pointer of the correct type
    myData = (double *) malloc(sizeof(double)*MAX);

    // 3. Free up the memory when you're done
    free(myData);
}

Final tips


  • Declare AND initialize any variables BEFORE you use them.
  • Write your explanations AFTER you write your code
    • Don't simply paraphrase what we told you to do!
    • Elaborate on how your code will tackle the problem
  • For functions, do NOT (re)-declare variables that have been passed as parameters
    • e.g., my_func(int x, double y) - x and y should not (and cannot) be declared inside the function body
  • Furthermore, VOID functions should not have any return statement; for any other return type, your function should return a value of the appropriate type (int, double, char, pointers, etc.)
  • Confirm whether you want to use data or a pointer to data
    • If I declare datatype *x, then *x is ALWAYS of type datatype, x is a pointer
    • If I declare datatype x, then x is ALWAYS of type datatype, &x is a pointer
    • Look for parallelism in the declaration and use!

Final Exam Review

By Grant R. Vousden-Dishington