Thursday, March 17, 2016

C Programming Tutorial - 1


Tutorial 1:
#define MYNAME "Chris Paul"
int globalVar = 100;
int main()
{
    /*
    char firstLetter = 'D';
    int age = 28;
    long int superInt = -327670000;
    double bitF = 3.123456789987654321;
    printf("This will print to screen\n\n");
    printf("I am %d years old\n\n", age);
    printf("SUper big int %ld\n\n", superInt);
    printf("Double = %.10f\n\n", bitF);
    printf("The first letter %c\n\n", firstLetter);
    printf("My name is %s\n\n", MYNAME);
    char myName[]="Chris Bosh";
    strcpy(myName, "Mario Chalmers");
    printf("Heat name is %s\n\n", myName);
    */
    /*
    char middle;
    printf("What is your middle?");
    scanf("%c", &middle);
    printf("Middle is %c", middle);
    char firstname[30], lastname[30];
    printf("What is your name?");
    scanf("%s %s", firstname, lastname);
    printf("Your name is %s %s\n\n", firstname, lastname);
    */
    int numx = 12;
    int numy = 14;
    printf("numx/numy : %f\n\n", (float)numx/numy);
    return 0;
}
Tutorial 2:
int main()
{
    /*
    int num1=1, num2=2;
    printf("Is 1>2=%d\n", num1>num2);
    int age=38;
    char *legalAge = (age>40)? "true": "false";
    printf("%s\n", legalAge);

    int num = 10;
    printf("I bought %s products \n\n", (num>1)? "many":"one");
    */
    /*
    int numhowbig;
    printf("How many bits?\n");
    scanf("%d", &numhowbig);
    printf("\n");
    int myincrementor = 1, mymultiplier = 1, finalvalue = 1;
    while(myincrementor < numhowbig){
        mymultiplier *=2;
        finalvalue = finalvalue + mymultiplier;
        myincrementor++;
    }
    if((numhowbig ==0) || (numhowbig ==1)){
        printf("Top value: %d\n\n", numhowbig);
    } else {
        printf("Top value %d\n\n", finalvalue);
    }
    */
    printf("\n");
    char sizeofShirt;
    do{
        printf("What size of shirt (S, M, L):\n");
        scanf("%c", &sizeofShirt);
    }while(sizeofShirt !='S' && sizeofShirt !='M' && sizeofShirt !='L');
    return 0;
}
Tutorial 3:
int globalVar = 0;

int add2Num(int a, int b){
    return a+b;
}
void changeVal(){
    int age = 10;
    globalVar =100;
    printf("Function dotor %d %d\n", age, globalVar);
}
int main()
{
    /* switch
    int whattodo;
    do{
        printf("\n");
        printf("1. What time is is?\n");
        printf("2. What is today date?\n");
        printf("3. What day is it?\n");
        printf("4. Quit\n");
        scanf(" %d", &whattodo);
    } while(whattodo<1 || whattodo>4);
    switch(whattodo){
        case(1) : printf("Print the time"); break;
        case(2) : printf("Print the date"); break;
        case(3) : printf("Print the day"); break;
        default: printf("Bye bye"); break;

    }
    */
    /* Array String
    int i;
    printf("\n");
    char wholename[12]="Derek Banas";
    int primeNum[3] = {2, 3, 5};
    int moreNum[] = {13, 15, 17, 19};
    printf("The first prime in the list is %d\n\n", primeNum[0]);
    char nextcity[] = "Paris";
    printf("%s", nextcity);

    char yourcity[30];
    printf("What city do you live?");
    //fgets(yourcity, 30, stdin);
    scanf("%s", yourcity);
    printf("Hello %s\n\n", yourcity);
    if(strcmp(nextcity, yourcity)==0)
    printf("Is your city Paris? %d\n", strcmp(nextcity, yourcity));
    char yourstate[] = ", Iowa";
    strcat(yourcity, yourstate);
    printf("Hello %s\n\n", yourcity);
    strcpy(yourcity, "Nalaikh, Ulaanbaatar");
    printf("Hello %s\n\n", yourcity);
    */
    int a = 50;
    globalVar = 11;
    printf("Anhnii utga: %d %d\n", a, globalVar);
    changeVal();
    printf("Function daraa %d %d\n", a, globalVar);
    return 0;

}
Tutorial 4:
void generateNum(int *rand1, int *rand2){
        *rand1 = rand()%50+1;
        *rand2 = rand()%50+1;
        printf("\nRand1: %d, Rand2: %d", *rand1, *rand2);
}
void editMess(char * message, int size){
    int i;
    char newMess[]="New message";
    if(size>sizeof(newMess)){
        for( i = 0; i<sizeof(newMess); i++){
            message[i]=newMess[i];
        }
    }
}
int main()
{
    /*
    int pNum[] = { 2, 3, 7 };
    printf("First index: %d\n",pNum[1]);
    printf("First index: %d\n", *(pNum+1));
    int i;
    char *students[4] = {"Sally", "Mark", "Paul", "Sue"};
    for( i = 0; i<4; i++)
        printf("%s: %d\n", students[i], &students[i]);
    */
    /*
    int rand1 = 0, rand2 = 0;
    generateNum(&rand1, &rand2);
    printf("\nRand1: %d, Rand2: %d", rand1, rand2);
    */
    char randMessage[] = "Edit my function";
    printf("Old Message: %s\n", randMessage);
    editMess(randMessage, sizeof(randMessage));
    printf("New message: %s\n", randMessage);
    return 0;

}
Tutorial 5-1:
struct dog{
    const char *name;
    const char *breed;
    int avgHeight;
    int avgWeight;
};
void getdogInfo(struct dog theDog){
    printf("\n");
    printf("Name: %s\n",theDog.name);
    printf("Breed: %s\n",theDog.breed);
    printf("Avg Height: %d\n",theDog.avgHeight);
    printf("Avg Weight: %d\n",theDog.avgWeight);
}
void getmemoryLocation(struct dog theDog){
    printf("\n");
    printf("Name Location: %d\n",theDog.name);
    printf("Breed Location: %d\n",theDog.breed);
    printf("Avg Height Location: %d\n",&theDog.avgHeight);
    printf("Avg Weight Location: %d\n",&theDog.avgWeight);
}
int main()
{
    struct dog cojo = {"Cujo", "Saint Bernard", 90, 264};
    getdogInfo(cojo);
    getmemoryLocation(cojo);
    return 0;

}
Tutorial 5-2:
struct dogFavs{
    char *food;
    char *frient;
};
typedef struct dog{
    const char* name;
    const char* breed;
    int avgHeight;
    int avgWeight;
    struct dogFavs favThings;

}dog;
void getdogFavs(dog theDog){
    printf("\n");
    printf("%s loves %s and his friend is %s\n\n", theDog.name, theDog.favThings.food,  theDog.favThings.frient);
}
void setdogWeight(dog *theDog, int newWeight){
    (*theDog).avgWeight = newWeight;
    printf("The weight was changed to %d\n\n", theDog->avgWeight);
}
int main()
{
    dog benji = {"Benji", "Silky Terrier", 25, 9, {"Meat", "Joe Camp"}};
    getdogFavs(benji);
    setdogWeight(&benji, 15);
    printf("The Weight in Main() %d\n", benji.avgWeight);
    return 0;

}

No comments:

Post a Comment