Monday October 21, 2002

The first thing I mentioned after explaining functions is why we put int as the return type of the main() function.  I stated the return value of the main() function is received by another program that calls your program.  In this class, you will not call another from your program.  In summary, instead of:

#include <stdio.h>
void main()
{
}

We do:

#include <stdio.h>
int main()
{
    return 0;
}

or better yet, the return value should be a variable:

#include <stdio.h>
int main()
{
    int returnValue = 0;

    return returnValue;
}

We continued on with functions today.  The first example I posted was asking your little brother or sister to go to the store and buy some groceries.  The function here is going to the store.  You gave your brother or sister some money and you expect change back.  I suggested you first write a framework for your application just to get your program to compile and to make sure all the function input parameters and return datatypes are correct.  This particular function takes two input parameters, one the money the user enters and the total amount of the groceries.

#include <stdio.h>

double goToStore( double moneyGiven, double totalAmount )
{
 double change = 0.0;

 return change;
}

int main()
{
 double amount = 0.0;
 double myChange = 0.0;

 printf("Enter the amount to give to little brother/sister.\n");
 scanf("%lf", &amount );
 myChange = goToStore(amount, 50.0);
 printf("My Change is:  %10.2lf\n", myChange);

 return 0;
}

OUTPUT:
Enter the amount to give to little brother/sister.
87.65
My Change is:        0.00


I then inserted the logic code for the function goToStore() by simply calculating the change amount:

#include <stdio.h>

double goToStore( double moneyGiven, double totalAmount )
{
 double change = 0.0;

 change = moneyGiven - totalAmount;
 return change;
}

int main()
{
 double amount = 0.0;
 double myChange = 0.0;

 printf("Enter the amount to give to little brother/sister.\n");
 scanf("%lf", &amount );
 myChange = goToStore(amount, 50.0);
 printf("My Change is:  %10.2lf\n", myChange);

 return 0;
}

OUTPUT:
Enter the amount to give to little brother/sister.
87.65
My Change is:       37.65


My last program involved calling a C built-in function called isalpha.  Remember, now you know about functions you can call other C functions that help you perform common operations on your program data.  I had a problem remembering what library to include in the program to use this function, it is ctype.h.  Hence, you need to do #include <ctype.h> to be able to use the function isalpha() which tells if a character is alphanumeric or not.  It takes as a parameter an integer and returns and integer.  The following code asks the user for a character, calls the built-in function isalpha() to check the character, then outputs to the user the result.

#include <stdio.h>
#include <ctype.h>
int main()
{
 char ch = '\0';
 int result = 0;

 while( 1 )
 {
  printf("Enter a character.\n");
  scanf("%c", &ch);
  result = isalpha(ch);
  if ( result == 0 )
   printf("Not an alpha.\n");
  else
   printf("It is an alpha.\n");
  // flush the return character
  scanf("%c", &ch);
 }
 return 0;
}

OUTPUT:
Enter a character.
a
It is an alpha.
Enter a character.
b
It is an alpha.
Enter a character.
5
Not an alpha.
Enter a character.

...infinite loop asking the user for a character


Wednesday October 16, 2002

I introduced how to declare a constant in  the C language.  To declare a constant, you use the #define statement.  As you recall, a statement prepended by a '#' is executed during the preprocessing stage of the program compilation.  The following is the example I used in class:

#include <stdio.h>
#define INTEREST_RATE 7.75
void main()
{
    float bank_balance = 0;

    bank_balance = INTEREST_RATE*2000;
    printf("Bank balance is:  $%.2f\n", bank_balance );
}

OUTPUT:
Bank balance is:  $15500.00


I then began chapter 5 with a brief explanation of functions.  I wrote a very simple function called square that does not return anything.
#include <stdio.h>

void square()
{
    int result = 0, num = 0;
    printf("Enter a number and I will square it.\n");
    scanf("%d", &num);
    result = num*num;
    printf("Square is:  %d\n", result);
}

void main()
{
    square();
}

OUTPUT:
Enter a number and I will square it.
7
Square is:  49
Press any key to continue


I could call the square function as many times as I wish.  The following code calls it infinite times.

#include <stdio.h>

void square()
{
    int result = 0, num = 0;
    printf("Enter a number and I will square it.\n");
    scanf("%d", &num);
    result = num*num;
    printf("Square is:  %d\n", result);
}

void main()
{
    while(1)
    {
        square();
    }
}

OUTPUT:
Enter a number and I will square it.
7
Square is:  49
Enter a number and I will square it.
9
Square is:  81
Enter a number and I will square it.
54
Square is:  2916
Enter a number and I will square it.
45
Square is:  2025
Enter a number and I will square it.

... and so on, an infinite loop ...



I also discussed the continue statement  in a for loop.  The continue statement simply tells the for loop to go to the next iteration with continuing with the next body statement.  I used the following example:

#include <stdio.h>

void main()
{
    int i = 0;
    for ( i = 0; i < 10; i++ )
    {
        if ( i == 5 )
            continue;
        printf("i is:  %d\n", i);
    }
}

OUTPUT:
i is:  0
i is:  1
i is:  2
i is:  3
i is:  4
i is:  6
i is:  7
i is:  8
i is:  9

Notice the above code skips printing the "i is:  5" but does the rest of the numbers

Monday October 14, 2002

In this lecture, I introduced the switch statement and the break statement in a loop.  The break statement simply ends a loop abruptly.  In other words, it jumps out of the current loop the break statement is in.  The following code reads every character.  If there are no 'a' characters in the text, then the loop will finish normally when the '\n' is reached and the condition becomes false.  However, if a 'a' character is reached it will break out of the loop and NOT continue picking up the rest of the characters.

#include <stdio.h>
void main()
{
    char ch = '\0';
    while( ch != '\n' )
    {
        scanf("%c", &ch);
        if ( ch == 'a' )
        {
            break;
        }
        printf("ch is:  %c\n", ch );
    }
    printf("I'm done with you.\n");
}

OUTPUT:
bac
ch is:  b
I'm done with you.


I then introduced the switch statement.  A switch statement is simply a way for us to write if - then statements in a short and compact way.  Remember, you must break during the body of a particular case otherwise the other cases will execute until a break statement is reached.

#include <stdio.h>
void main()
{
    char ch = '\0';
    int aCount = 0, bCount = 0, otherCount = 0;
    while( ch != '\n' )
    {
        scanf("%c", &ch);
        switch( ch   )
        {
        case 'a':
            aCount++;
            break;
        case 'b':
            bCount++;
            break;
        default:
            otherCount++;  // this will include the newline character
            break;
        }
    }
    printf("number of a's is:  %d\n", aCount );
    printf("number of b's is:  %d\n", bCount );
    printf("number of others is:  %d\n", otherCount );
}

OUTPUT:
aabbcc
number of a's is:  2
number of b's is:  2
number of others is:  3



Notice if you don't put the break statement in case 'a' then the b's will get extra count.  The following is a sample without the break statement:

#include <stdio.h>
void main()
{
    char ch = '\0';
    int aCount = 0, bCount = 0, otherCount = 0;
    while( ch != '\n' )
    {
        scanf("%c", &ch);
        switch( ch   )
        {
        case 'a':
            aCount++;
        case 'b':
            bCount++;
            break;
        default:
            otherCount++;  // this will include the newline character
            break;
        }
    }
    printf("number of a's is:  %d\n", aCount );
    printf("number of b's is:  %d\n", bCount );
    printf("number of others is:  %d\n", otherCount );
}

OUTPUT:
aabbcc
number of a's is:  2
number of b's is:  4
number of others is:  3


We then finished up the loops.  The following demonstrates an example of a do-while loop that prints the name two times.  Remember, a do-while loop is guaranteed to execute at least once.  The condition is tested AFTER the first iteration of the loop.

#include <stdio.h>
void main()
{
    int count = 0;
    do {
        // statements
        printf("My name is Goofy.\n");
        count++;
    }
    while(  count < 2   );
}
 

OUTPUT:
My name is Goofy.
My name is Goofy

The following is the same as above except it uses a while loop to print the name twice.  A while loop checks the condition before the  FIRST iteration of the loop statement.

#include <stdio.h>
void main()
{
    int count = 0;
    while ( count < 2 )
    {
        printf("My name is Goofy.\n");
        count++;
    }
}
 

OUTPUT:
My name is Goofy.
My name is Goofy
 

Wednesday October 2, 2002

I discussed some interesting side effects when reading characters. In addition, I discussed the use of the function getchar().

I started with the program below. The expected behavior is the program will read in a character, output the character back to the console and then wait for another character. However, this is not what happens. Specifically, when the input character is entered, a "return" character also gets inputted. This means if I input character 'a', then what the computer sees is the following: a<return>

The 'a' is a character and so is the '<return>' which is consider the new line character with ASCII decimal value of 10.


#include <stdio.h>
void main()

{

    char ch='\0'; // null

   while( 1 )

      {

               scanf("%c", &ch);

               printf("Your character is: %c\n", ch);

  }

}



OUTPUT:

a
Your character is:  a
Your character is:



The way we solved the above problem is we simply flushed out the <return> character from memory by reading the character and not doing anything with it. We do this for every character the user enters. When you read a character using scanf(), the character is removed from computer memory. The new program looks something like this:


#include <stdio.h>
void main()

{

    char ch='\0'; // null

   char return_char;

       while( 1 )

      {

               scanf("%c", &ch);

               printf("Your character is: %c\n", ch);

          // flush the return character

           scanf("%c", &return_char); // no value but to flush memory

      }

}
OUTPUT:

a
Your character is: a
b
Your character is: b
c
Your character is: c


We then went on to investigate how to use the getchar() function. This function is simply a fast way to do a scanf("%c"). Utilize the getchar() function to get characters. You will need to use this function for assignment 2.


#include <stdio.h>
void main()

{

    char ch='\0'; // null
        while( (ch = getchar()) >= 0 )

  {

               printf("Your character is: %c (ASCII = %d)\n", ch, ch);

         ch = getchar(); // flush the return character

   }

}
OUTPUT:

a
Your character is: a
b
Your character is: b
c
Your character is: c


The following reads characters from user until a <return> character is pressed. Recall, a <return> character is ASCII value 10.


#include <stdio.h>
void main()

{

    char ch='\0'; // null
        while( (ch = getchar()) != 10 )

 {

               printf("Your character is: %c (%d)\n", ch, ch);

 }

}
OUTPUT:

abcde
Your character is: a (97)
Your character is: b (98)
Your character is: c (99)
Your character is: d (100)
Your character is: e (101)
Press any key to continue

Monday September 30, 2002

I discussed the while loop today.

The following program will output "howdy" exactly 5 times.


#include <stdio.h>
void main()

{

   int counter=0;
   // counter controlled loop

   while ( counter < 5 )

   {

     printf("howdy\n");

     counter = counter + 1;
   }

}




The following program is like the above except it uses the increment operator to increment the counter.


#include <stdio.h>
void main()

{

   int counter=0;
   // counter controlled loop

   while ( counter < 5 )

   {

     printf("howdy\n");

     counter++; // same as counter = counter + 1
   }

}


The following program is like the above except it uses the increment operator to increment the counter by 2. In this case, it will only print howdy 3 times instead of 5.


#include <stdio.h>
void main()

{

   int counter=0;
   // counter controlled loop

   while ( counter < 5 )

   {

     printf("howdy\n");

     counter += 2;  // same as counter = counter + 2
   }

}

Wednesday September 25, 2002

The following code prints infinitely.

#include <stdio.h>
void main()

{

  // infinite loop

   while( 1 )

   {

      printf("I can't stop. You can't catch me.\n");

   }
 }



OUTPUT:


I can't stop. You can't catch me.

I can't stop. You can't catch me.

I can't stop. You can't catch me.

I can't stop. You can't catch me.

<on and on and on, never stops>


The following program takes as input a character and outputs its corresponding ASCII value.


#include <stdio.h>
void main()
{
  char ch='\0';  // NULL value
   printf("Enter a character.\n");

   scanf("%c", &ch);

   printf("Your character is %c and ascii value is %d\n", ch, ch);

}

OUTPUT:



Enter a character.
A
Your character is A and ascii value is 65
Press any key to continue



The following program takes as input an integer and checks if it is greater than 10.


#include <stdio.h>

void main()
{
   int num = 0;
   printf("Enter a number.\n");

   scanf("%d", &num);
   if( num > 10 )

   {

       printf("I got you. You are greater than 10.\n");

   }

}
OUTPUT:

Enter a number.
25
I got you.  You are greater than 10.
Press any key to continue

Same thing as above except it will output the actual condition value. Recall the condition value evaluates to a 0 or non-zero in C. 0 means false and non-zero means true.


#include <stdio.h>

void main()
{
   int num = 0;
   printf("Enter a number.\n");

   scanf("%d", &num);
   printf("Condition:  %d\n", (num > 10) );

   if( num > 10 )

   {

       printf("I got you. You are greater than 10.\n");

   }

}
OUTPUT:

Enter a number.
25
Condition:  1
I got you.  You are greater than 10.
Press any key to continue




The following program checks if the input integer is between 10 and 15 but not including the 10 and the 15.


#include <stdio.h>

void main()
{
        int num = 0;
        printf("Enter a number.\n");

    scanf("%d", &num);
        if( (num > 10) && (num < 15) )

          {

               printf("Your number is between 10 and 15.\n");

          }

}
OUTPUT:

Enter a number.
12
Your number is between 10 and 15.
Press any key to continue




The following program checks if the input integer is between 10 and 15 but not including the 10 and the 15. If it is not, it will perform an "else" part to the "if" statement and print the number is out of range.


#include <stdio.h>

void main()
{

        int num = 0;
        printf("Enter a number.\n");

    scanf("%d", &num);
        if( (num > 10) && (num < 15) )

          {

               printf("Your number is between 10 and 15.\n");

          }

       else 

           {

               printf("Your number is outside the range.\n");

          }

}
OUTPUT:

Enter a number.
6
Your number is outside the range.
Press any key to continue

September 18, 2002

I demonstrated the following examples in class today. They should help you with lab assignment 1.

#include <stdio.h>
void main()



{



    int num1 = 0;



   printf("Enter an integer.\n");



  scanf("%d", &num1);



     printf("You entered %d\n", num1);



}







OUTPUT:



Enter an integer.
897
You entered 897








The following program takes as input two numbers, calculates the sum, and outputs the sum.

#include <stdio.h>

void main()



{



   int num1 = 0;



   int num2 = 0;



   int sum = 0;
   printf("Enter two numbers.\n");



   scanf("%d%d", &num1, &num2);



   sum = num1 + num2;



   printf("Sum of the two input numbers is %d\n", sum);



}







OUTPUT:



Enter two numbers.
90 45
Sum of the two input numbers is 135








The following program takes as input an integer and a double, does a real division, and outputs the real division result. Please note that the result output is not being formatted to any number of decimal places. The default number of decimal places is determined by the printf statement.
 

#include <stdio.h>
void main()



{



   int num1 = 0;



   double num2 = 0;



   double div = 0;
   printf("Enter two numbers.\n");



   scanf("%d%lf", &num1, &num2);



   div = num1/num2;



   printf("Real division of the two numbers is %lf\n", div);



}











OUTPUT:



Enter two numbers.
22 7.0
Real division of the two numbers is 3.142857





The following program takes as input an integer and a double, does a real division, and outputs the real division result. It is basically the same as above with the exception that the result output is formatted to two decimal places. This is accomplished by the use of the "%.2lf". The ".2" tells printf to output to two decimal places. If you wanted only one decimal place then use "%.1lf".





#include <stdio.h>
void main()



{



   int num1 = 0;



   double num2 = 0;



   double div = 0;
   printf("Enter two numbers.\n");



   scanf("%d%lf", &num1, &num2);



   div = num1/num2;



   printf("Real division of the two numbers is %.2lf\n", div);



}











OUTPUT:



Enter two numbers.
22 7.0
Real division of the two numbers is 3.14

The following program takes as input two integers, calculates the quotient and remainder of a division operation, and outputs the result.

#include <stdio.h>

void main()



{



   int num1 = 0;



   int num2 = 0;



   int div = 0;



   int remainder = 0;
   printf("Enter two numbers and I will divide them.\n");



   scanf("%d%d", &num1, &num2);



   div = num1/num2;



   remainder = num1%num2;



   printf("%d with a remainder of %d\n", div, remainder);



}
OUTPUT:



Enter two numbers and I will divide them.
22 7
3 with a remainder of 1












The following program calculates the slope of a line using the equation y = mx + b. It asks for the values of m, x, and b then calculates the y.



#include <stdio.h>
void main()



{



   int y = 0;



   int m = 0;



   int x = 0;



   int b = 0;
   printf("This program calculates the slope of a line using equation y    = mx + b\n");



   printf("Enter the m, x and b separated by a space.\n");



   scanf("%d%d%d", &m, &x, &b);



   y = m*x + b;



   printf("y is %d\n", y);



}
OUTPUT:



This program calculates the slope of a line using equation y = mx + b
Enter the m, x and b separated by a space.
5 6 7
y is 37








 



September 11, 2002

We began the class with the basic framework of a C program. The following is the simplest program you can write. It does nothing but it does compile and execute.

#include <stdio.h>
void main()



{



}

We also wrote another basic program with using the keyword "int" instead of "void". This is what the class text demonstrates.


#include <stdio.h>
int main()



{



   return 0;



}

We then expanded the program with the use of printf function.


#include <stdio.h>
int main()



{



   printf("Hello");



   return 0;



}




Lastly, we wrote a program that takes as input two numbers and adds them together. In this program we are introducing the the scanf function that takes input from the user. Please note that you may use a "%i" or a "%d" to grab integers from the user.


#include <stdio.h>
int main()



{



     int num1;



       int num2;
        printf("Welcome to my program.\n");



     printf("Please enter two integers.\n");



         scanf("%i%i", &num1, &num2 );



           printf("Result is %i\n", num1 + num2 );



         return 0;



}