Sunday, March 20, 2016

C Programming Tutorial - 3

Tutorial 9 Linked List Full example
struct product{
    float price;
    char productname[20];

    struct product *next;
};
struct product *pFirstNode = NULL;
struct product *pLastNode = NULL;
void createNewList(){
    struct product *pNewstruct = (struct product *) malloc(sizeof(struct product));
    pNewstruct->next = NULL;
    printf("Enter Product Name:");
    scanf("%s", &(pNewstruct)->productname);
    printf("Enter Product Price:");
    scanf("%f", &(pNewstruct)->price);
    pFirstNode = pLastNode = pNewstruct;
}
void inputData(){
    if(pFirstNode == NULL){
        createNewList();
    } else {
    struct product *pNewstruct = (struct product *) malloc(sizeof(struct product));
    printf("Enter Product Name:");
    scanf("%s", &(pNewstruct)->productname);
    printf("Enter Product Price:");
    scanf("%f", &(pNewstruct)->price);
    if(pFirstNode == pLastNode){
        pFirstNode->next =  pNewstruct;
        pLastNode = pNewstruct;
        pNewstruct->next = NULL;
    } else {
        pLastNode->next = pNewstruct;
        pNewstruct->next = NULL;
        pLastNode = pNewstruct;
    }
    }
}
void outputData(){
    struct product *pProducts = pFirstNode;
    printf("Products Entered\n");
    while(pProducts!=NULL){
        printf("%s costs %.2f\n\n", pProducts->productname, pProducts->price);
        pProducts = pProducts->next;
    }
}
struct product *pProductbeforeProducttoDelete = NULL;

struct product *searchForProduct(char * productname){
    struct product *pProductIterator=pFirstNode;
    while(pProductIterator!=NULL){
        int areThey = strncmp(pProductIterator->productname, productname, 30);
        if(!areThey){
            printf("%s was found and it costs %.2f\n\n",pProductIterator->productname, pProductIterator->price);
            return pProductIterator;
        }
        pProductIterator = pProductIterator->next;
    }
    pProductbeforeProducttoDelete = pProductIterator;
    printf("%s wasnt found\n\n", productname);
    return NULL;
}
void removeProduct(char *productname){
    struct product *pProductToDelete = NULL;
    pProductToDelete = searchForProduct(productname);
    if(pProductToDelete!=NULL){
        printf("%s was deleted\n\n", productname);
        if(pProductToDelete==pFirstNode){
            pFirstNode = pProductToDelete->next;
        } else {
            pProductbeforeProducttoDelete->next = pProductToDelete->next;
        }
        free(pProductToDelete);
    } else {
        printf("%s was not found", productname);
    }
}
int main(){
    inputData();
    inputData();
    inputData();
    outputData();
    removeProduct("Tomato");
    outputData();
    return 0;
}
Tutorial 10 File I/O
int main()
{
    char buffer[1000];
    int num, i;
    FILE *pFile;
    pFile = fopen("randomnumbers.txt","w");
    if(!pFile){
        printf("Error: Couldn't write to file");
        return 1;
    }
    for(i=0; i<10; i++){
        num = rand()%100;
        fprintf(pFile, "%d\n", num);
    }
    printf("Success Writing to File\n");
    if(fclose(pFile)!=0){
        printf("Error: File not Closed\n");
    }
    return 0;
}

C Programming Tutorial - 2

Tutorial 6 Linked List Example:
typedef struct product{
    const char * name;
    float price;
    struct product *next;
} product;

void printLinkedList(product *pProduct){
    while(pProduct != NULL){
        printf("A %s costs %.2f\n\n", (*pProduct).name, pProduct->price);
        pProduct = pProduct->next;
    }
}
int main()
{
    product tomato = {"Tomato", .51, NULL};
    product potato = {"Potato", 1.21, NULL};
    product lemon = {"Lemon", 1.69, NULL};
    product milk = {"Milk", 3.09, NULL};
    product turkey = {"Turkey", 1.86, NULL};

    tomato.next = &potato;
    potato.next = &lemon;
    lemon.next = &milk;
    milk.next = &turkey;

    product apple = {"Apple", 1.58, NULL};

    lemon.next = &apple;
    apple.next = &milk;

    printLinkedList(&tomato);
    return 0;

}
Tutorial 7 Strings and Functions
void makeLower(char * theString){
    int i = 0;
    while(theString[i]){
        theString[i]= tolower(theString[i]);
        i++;
    }
}
int main()
{

    _Bool isaNum = 1;
    int num;
    int sumofnum=0;

    do{
    printf("Enter a number:");
    isaNum = (scanf("%d", &num)==1);
    if(isaNum)  sumofnum += num;
    } while( isaNum );
    printf("The Sum is: %d\n\n", sumofnum);

    char theChar;
    while((theChar = getchar())!='~'){
        putchar(theChar);
    }

    char name[50];
    printf("What is your name?");
    fgets(name, 50, stdin);
    fputs("Hi ", stdout);
    fputs(name, stdout);
    return 0;
}
Tutorial 8 Malloc()
struct product{
    float price;
    char productname[20];
};
int main(){
    struct product * pProducts;
    int i, j;
    int numofPro;
    printf("Enter the number of products to store:");
    scanf("%d", &numofPro);
    pProducts = (struct product*) malloc(numofPro *sizeof(struct product));
    for(i = 0; i<numofPro; i++){
        printf("Enter a product name:");
        scanf("%s", &(pProducts+i)->productname);
        printf("Enter a product price:");
        scanf("%f", &(pProducts+i)->price);
    }
    printf("Products Stored\n");
    for(i = 0; i<numofPro; ++i){
        printf("%s\t%.2f\n", (pProducts+i)->productname, (pProducts+i)->price);
    }

    free(pProducts);
    return 0;
}

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;

}