User:Amy Suo Wu/C Syntax

From XPUB & Lens-Based wiki

nested loop orgy!

assignment: write hello world -->stdio ( standard in and out)


#include "stdio.h"

char word []= "world";    

int main () {


printf("hello %s\n", word); 

// for the compiler to know when new functions need to be execute. eg. spaces in between words. 

}

assignment: call the second character

#include "stdio.h"

char string []= "hello world\n";    

int main () {


printf("%c\n", string[1]); 

 
}

assignment: vertically type the text

#include "stdio.h"


char pat []= "hello world\n";    
int i = 0;

int main () {
    while (i<12) { // always needs an extra curly braces!!
        printf("%c\n", pat[i]);
        i = i + 1; // is the same as i++
    } // always needs an extra curly braces!!
}

assignment: print 2 columns, with an increasing of set

#include "stdio.h"


char pat []= "X_X_X_XXXX_XXX";    
int i = 0;

int main () {
    while (i<12) { 
        printf("%c %c\n", pat[i], pat[i]);
        i = i + 1; 
    } 
}


assignment: step 1 nested loop, printed 12 times. shifting sets.

#include "stdio.h"
#include "string.h"

char pat []= "XXX XX X XX ";    
int i = 0;
int j = 0;

int main () {
    while (i<12*12) { 
        printf("%c%c\n", pat[i%12], pat[j%12]); // %  means between 0-12. modules is last interger left over from division. (specific maths function)
        i = i + 1; 
        }
        
        while (i<12) { 
        printf("poop");       

        j = j + 1; 
        }
 
}

assignment: step2 nested loop, printed 12 times. shifting sets.

#include "stdio.h"
#include "string.h"

char pat []= "XXX XX X XX ";    
int i = 0;
int j = 0;

int main () {

    while (j < 13){ //repeat 13 times

        while (i<12) { // repeat if less than 12
            printf("%c%c\n", pat[i], pat[(i+j)%12]);  
            i = i + 1; // to keep on adding 1 to the value of 'i'
        }
        i = 0; // redefine the value before it loops back to the main loop
        j = j + 1; // to keep on adding 1 to the value of 'j'
        


        }


}