COMP1911 17s1 Code Examples from Lectures on revision Introduction to Programming

Read in 10 numbers using getchar.

May have many spaces between them or not.

If not enough numbers are read in, should be an error

Negative numbers or anything else should be an error

Note: this is to show you how to skip spaces and read in ints with getchar(). Usually if you were just reading in ints you would use scanf with %d.

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX 10

void printData(int data[], int size);
int readData(int data[], int maxSize);

int main(void){
    int nums[MAX];
    int numRead = readData(nums,MAX);
           
    if ( numRead != MAX){
        printf("Invalid\n");
        return EXIT_FAILURE;
    }
    printData(nums,MAX);
    
    return EXIT_SUCCESS;  
}

// Valid input examples

// 1 4 789 23 12 45 12 5 4 3

//     1      4 789 23 12 45 12   5 4      3

// We could break this function down into further functions
// for example we could write a function to read an integer 
int readData(int data[], int maxSize){
    int i = 0;
    int valid = 1;
    int c;
    int num = 0;
        
    c = getchar();
    while ( i < MAX && valid == 1 && c != EOF ){
       while(isspace(c)){
          c = getchar();
       }
       //read in a letter
       
       //skip more space
       if(isdigit(c)){
           num = 0;
           while(isdigit(c)){  
               
              num = num * 10 + c - '0';
              c = getchar();
           }
           data[i] = num;
           i++;
       } else {
           valid = 0;
       }
    }
    return i;
}


void printData(int data[], int size){
    int i = 0;
    
    while ( i < size ){
        printf("%d ",data[i]);
        i = i + 1;
    }
    printf("\n");
}

Some rbasic evision examples

What do these programs do?

#include <stdio.h>

int main(void) {
    int a = 25;
    if (a < 50) {
        if (a > 25) {
            printf("1\n");
        } else {
            printf("2\n");
        }
    } else {
        if (a < 75) {
            printf("3\n");
        } else {
            printf("4\n");
        }
    }
    return 0;
}

#include <stdio.h>

int main(void) {
    int a = 42;
    if (a < 25) {
        printf("1\n");
    }
    if (a < 50) {
        printf("2\n");
    }
    if (a < 75) {
        printf("3\n");
    }
    return 0;
}

#include <stdio.h>

int main(void) {
    int a = 42;
    if (a < 25) {
        printf("1\n");
    } else if (a < 50) {
        printf("2\n");
    } else if (a < 75) {
        printf("3\n");
    }
    return 0;
}

#include <stdio.h>

int main(void) {
    int a = 47;
    if (a < 25) {
        printf("1\n");
    }
    if (a < 50 && a > 45) {
        printf("2\n");
    }
    if (a < 75 || a > 70) {
        printf("3\n");
    }
    return 0;
}

#include <stdio.h>

int main(void) {
    int x = 30;
    int y = 50;
    int i;

    i = x;
    while (i <= y) {
        if (i % 7 == 0) {
            printf("%d\n", i);
        }
        i = i + 1;
    }
    return 0;
}

#include <stdio.h>
int main(void) {
    int s = 0;
    int t = 2048; 
    while (t != 0) {
       s = s + t % 10;
       t = t / 10;
    }
    printf("%d %d\n", s, t);
    return 0;
}

#include <stdio.h>
#include <string.h>

#define MAX 100

int main(void) {
    char str[MAX];
    int i, j, temp;
                 
    strcpy(str, "live"); 
                                  
    i = 0;
    j = strlen(str) - 1;
    while (i < j) {
        temp = str[i];
        str[i] = str[j];
        str[j] = temp;
        i = i + 1;
        j = j - 1;
    }

    printf("Str contains: %s \n", str);
    return 0;
}

Calculate the mathematical constant pi by summing the series 4 - 4/3 + 4/5 - 4/7 + 4/9 + ...

#include <stdio.h>


#define N_SERIES_TERMS 1000000

int main(void) {
    int n;
    double sum;

    n = 0;
    sum = 0;
    while (n < N_SERIES_TERMS) {
        if (n % 2 == 0) {
            sum = sum + 4.0 / (2 * n + 1);
        } else {
            sum = sum - 4.0 / (2 * n + 1);
        }
        n = n + 1;
    }

    printf("4 - 4/3 + 4/5 - 4/7 + 4/9 + ... = %f\n", sum);
    return 0;
}

SOLUTION TO THIS WILL BE ADDED IN THE NEXT WEEK

CHECK BACK IF YOU WANT TO SEE IT

This is the incomplete and possibly buggy version we were working on in class.

This program should expect 2 command line arguments.

The first argument should be the name of a file that your program should read from and the second should be the name of a file that your program should write to.

Your program should read a search string from standard input.

Your program should then print all of the lines from the input file to the output file, with all occurrences of the search string removed. The input file should remain unchanged.

We are doing case sensitive matching.

If the wrong number of command line arguments are given, or a file can't be opened, a suitable message should be printed to standard error.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_KEY 15
#define MAX_LEN 1026


int matches(char in[],int i,char key[]);
void processData(char key[], FILE * fIn, FILE * fOut);
// argc = 3
//       0      1       2  
// ./remove f.txt out.txt


int main(int argc, char * argv[]){
    char key[MAX_KEY]; //Sing
      
    FILE * fileIn;
    FILE * fileOut;
   
    //Check there are command line arguments 
    if ( argc < 3){
        fprintf(stderr,"Usage: ./remove fname1 fname2\n");
        return EXIT_FAILURE;
    }
    
    //Open file for reading and check it worked
    fileIn = fopen(argv[1],"r");
    if( fileIn == NULL){
        fprintf(stderr,"could not open file\n");
        return EXIT_FAILURE;
    }
    
    //Open file for writing and check it worked
    fileOut = fopen(argv[2],"w");
    if( fileOut == NULL){
        fprintf(stderr,"could not open file\n");
        return EXIT_FAILURE;
    }
    
    //Read in string to search for and remove
    printf("Enter string to remove: ");
    fgets(key,MAX_LEN,stdin);
    int len = strlen(key);
    if(len > 0 && key[len-1] == '\n'){
        key[len -1] = '\0'; //get rid of trailing newline
    }
    
    //Read in each line of input from the file and 
    //remove the key if it exists
    processData(key,fileIn,fileOut);
    
    //Close the files
    fclose(fileIn);
    fclose(fileOut);
    return EXIT_SUCCESS;
}

void processData(char key[], FILE * fIn, FILE * fOut){
    char in[MAX_LEN];  //One line of input from the file
    //         Sing
    //One. Singular sensation
    while(fgets(in,MAX_LEN,fIn) != NULL){
        i = 0;
        while ( in[i] != '\0'){
            if(matches(in,i,key)){
               i = i + strlen(key);
            } else {
               fprintf(fOut,"%c",in[i]);
               i++;
            }
        }
    }
}
//LEFT AS AN EXERCISE
int matches(char in[],int i,char key[]){
    

}