Showing posts with label Basic C. Show all posts
Showing posts with label Basic C. Show all posts

Structures

Structures are a collection of variables related in nature (those variables can be different data types)

Question: How to create a structure?
Answer: Using "struct" keyword to build the structure definition (place it before and outside the main method; don't forget semicolon at the end)
Example:
struct record{
     char lastName[20];
     char firstName[20];
     int score;
};

record is now a structure tag which can be used to create instances of the structures

Simple program sample:

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

struct student{
char fName[10];
char lName[10];
int id;
float tuition;
};

int main(void){
/*CREATE INSTANCE OF STUDENT STRUCTURE*/
struct student s1;

/*ASSIGN VALUES TO MEMBERS*/
strcpy(s1.fName,"Thanh");
strcpy(s1.lName,"Lai");
s1.id = 789 ;
s1.tuition = 8801.50;

/*PRINT THE MEMBER CONTENTS*/
printf("\nFirst Name: %s", s1.fName);
printf("\nLast Name: %s", s1.lName);
printf("\nStudent ID: %d", s1.id);
printf("\nTuition: %f", s1.tuition);

return 0;
}


Output:

Information hiding

Information hiding is a conceptual process by which programmers conceal implementation details into functions.
Functions can be seen as black boxes.
Black boxes are simply components, logical or physical that perform tasks.
You may not know how the black box performs (implements) tasks; you just simply know it works when needed.
Each black box describes one component;
in this case the components are printf() and scanf(). The reason that I consider the two
functions printf() and scanf() black boxes is because you do not need to know what’s inside
of them (how they are made), you only need to know what they take as input and what they
return as output. In other words, understanding how to use a function while not knowing
how it is built is a good example of information hiding.

In structured programming you build components that can be reused (code reusability) and
that include an interface that other programmers will know how to use without needing to
understand how they were built (information hiding).

Question 3

Question: Write a program that capitalizes the first letter of each word from a input string
Answer: 

#include <stdio.h>
#include <ctype.h>                               /*The ctype.h header file of the C Standard Library provides                                                                                                             declares several functions useful for testing and mapping characters. */
int main(void){
    int c;
    int alreadyUp = 1;  /*Check if the previous character is capitalized or not*/
                        /*Assume that: 1 is not uppercase yet and 2 is already uppercase*/

    /*Create an infinite loop*/
    for( ; ; ){
        c = getchar();
        if(c == EOF) break;

        switch (alreadyUp){
        case 1:
            if(isalpha(c)){                 /*if c is alpha and not uppercase yet*/
                putchar(toupper(c));
                alreadyUp = 2;              /*set the contraint to be 2 which is already uppercase*/
            } else putchar(c);              /*blank space ONLY*/
            break;
        case 2:
            if(!isalpha(c)){
                putchar(c);
                alreadyUp = 1;
            } else putchar(c);
            break;
        default:
            printf("This should not be showed in the result");
        }
    }
    return 0;
}


Output:


Question 2

Question: Write a program that convert alphabet characters to be upper-cased
Answer:
Before we go to code this program, we wonder that how can the computer knows what characters are uppercase or lowercase
Thanks to ASCII, we have

The design of this program will be almost similar to the question 1; other words, we just need to add a small 'function' into the infinite loop

#include <stdio.h>

int main(void){
int c;
for( ; ; ){
c = getchar();
if(c == EOF)
break;
if( (c >= 97) && (c <= 122) ) /*if it is lowercase*/
c -= 32;                 /*meaning new c = old c - 32 (gap)*/
putchar(c);
}
return 0;
}

Output:




Question 1

Question: Create a C program that reads and writes user's input

Answer:

/*include I/O function for compiler to check*/
#include <stdio.h>

int main(void){
    int c;
    for ( ; ; ){           /*infinite loop*/
    c = getchar();     /*read  a next single character from stdin*/
    if(c == EOF)     /*EOF (end-of-file) is global constant, which is defined in stdio.h  */
        break;           /*if end-of-file, break out of the loop*/
    putchar(c);        /*else, write a single character to stdout*/
    }
    return 0;
}

Output:





There are many other ways to do it



Basic C

This section is all about C programming language
 

Copyright © 2013-2014 Lai Duy Thanh All rights reserved.