User Tools

Site Tools


Sidebar

projects

haas:spring2014:cprog:projects

This is an old revision of the document!


Corning Community College

CSCS1320 C/C++ Programming

Assignments, Documents, Information, and Projects

Projects

Week 6

  • arrays
    • pointer arithmetic
  • loops
    • for
    • while
    • do while
1
#include <stdio.h>
#include <string.h>
 
int main(int argc, char **argv)
{
    int i, j;
 
    fprintf(stdout, "You typed: ");
    for(i = 0; i < argc; i++)
        fprintf(stdout, "%s ", argv[i]);
    fprintf(stdout, "\n\n");
 
    for(i = 0; i < argc; i++)
        fprintf(stdout, "*(argv+%d) / argv[%d]: %s\n", i, i, argv[i]);
    fprintf(stdout, "\n\n");
 
    for(i = 0; i < argc; i++)
    {   
        for(j = 0; j <= strlen(argv[i]); j++)
        {   
            if ((*(*(argv+i)+j)) == '\0')
                fprintf(stdout, "*(*(argv+%d)+%d): '\\0' ", i, j); 
            else
                fprintf(stdout, "*(*(argv+%d)+%d): '%.2c'  ", i, j, (*(*(argv+i)+j)));
 
            fprintf(stdout, "(%.3d / 0x%2.2X)\n", (*(*(argv+i)+j)), (*(*(argv+i)+j)));
        }   
        fprintf(stdout, "\n");
    }   
    fprintf(stdout, "\n");
 
    return(0);
}

Week 5

  • selection statements
    • if/else if/else
    • switch/case
  • value of taking the modulus (remainder) of a number
    • with integers, math has no decimal points– such values are dropped/truncated
    • 4 % 6 = 4
    • 22 % 8 = 6
    • 22 / 8 = 2
    • 4 / 6 = 0
  • pointers (briefly– ask more and again)
    • * - dereferences (looks up what is at the memory address contained in the pointer)
    • & - address of (looks up the address of the variable in memory)
    • Pointer-related video: Pointer Fun with Binky
  • on lab46, if you see a C function you don't know, try looking up its manual page:
    • for printf(): man 3 printf
    • for atoi(): man 3 atoi
    • for rand(): man 3 rand
      • this is good for finding out what addition header files need to be included
  • did some code walk-throughs/desk checking.
    • for the following programs, work out by hand the values for i and m when x is:
      • 1
      • 2
      • 3
      • 4

Sample code 1

#include <stdio.h>
#include <stdlib.h>
 
int main()
{
    int i, x, m = 0;
    srand(time(NULL));
    x = rand()%4 +1;
    for(i=0;i<x;i++)
    {
        m = m + i + x;
    }
    if(m<8)
        i=7;
    else if(m==16)
        i=4;
    else if((m%2)==1)
        i=33;
    else
        i=6;
 
    printf("%d\n", m);
    return(0);
}

Sample code 2

#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char **argv)
{
    int m, i, x;
    if(argc <2)
    {
        fprintf(stderr, "Error!\n");
        exit(1);
    }
    x= atoi(argv[1]);
    for(i=0;i<x;i++)
    {
        m = m+i+x;
    }
    m=m%8;
 
    switch(m)
    {
        case 0:
            i=3;
            break;
        case 1:
        case 2:
            i=12;
            break;
        case 4:
        case 7:
            i=7;
            break;
        case 5:
            i=2;
            break;
        default:
            i=0;
            break;
    }
    printf("%d\n", m);
    return(0);
}

Week 4

  • WEATHER happened. People still showed up, and we talked about the data type project for a bit, as well as the squares project, and while we did go over new stuff (if statements), I plan to review if() statements again next week.
  • Data Types, continued
  • reviewed squares project
    • input with scanf()
    • 35^2 = 3*4 25 = 1225
    • use simple math expressions to manipulate your input (115 OPERATION NUMBER = 11)
  • if()/selection statements – let the computer make informed decisions, so long as that decision is true or false.
    • if() statements evaluate a condition. Conditions can be a number, or they can be the result of some relational expression. Operators are:
      • == (is equal to)
      • != (is not equal to)
      • < (is less than)
      • ⇐ (is less than or equal to)
      • > (is greater than)
      • >= (is greater than or equal to)
    • a common mistake is to put a single equal sign (=) in a condition.
      • this doesn't check equality, it sets it, and setting is always true
    • using && and ||, we can have compound if() statements
    • if you use an if(), you can have:
      • at most one if() statement
      • 0 or more else if() statements
      • 0 or 1 else statements
  • Looked briefly at ASCII characters and their numeric representation ('A' is 65, space is 32, 'a' is 97, '0' is 48)
  • showed some more mental math
int number = 0;
 
printf("Enter a number (0-10): ");
scanf("%d", &number);
 
if (number < 0)
{
    printf("Error, value is less than 0!\n");
}
else if (number == 1)
{
    printf("ONE!\n");
}
else if ((number <= 10) && ((number % 2) == 0)) // detect even number (compound if)
{
    printf("Even number of %d\n", number);
}
else if ((number == 3) || (number == 7)) // compound if using OR connective
{
    printf("you entered a %d\n", number);
}
else if ((number > 4) && (number < 10)) // compound if using AND connective
{
    printf("remaining odd number of %d\n", number); // how will this only hit 5 or 9?
}
else
{
    printf("value is greater than 10!\n");
}

Week 3

  • Signed values
    • how to represent
    • how to manually encode
      • one's complement (why is this problematic?)
      • two's complement
        • invert, then add 1
    • what this does to the range
    • impact on resulting quantity
  • discuss next week's project: squares
    • input with scanf()
      • need to pass variables by address
      • formatted text string same as with printf()

Week 2

  • We covered some C programming details relating to the data types project:
    • printf/fprintf
      • STDIN/STDOUT/STDERR
      • format string specifiers
        • %d
        • %u
        • %ld
        • %lu
        • %hd
        • %hu
        • %hhd
        • %hhu
        • %c
        • %s
        • the space allocation/zero padding optional value that can be specified within the format string specifier
    • sizeof() function
      • relatedly:
      • low and high values within a fixed size range
      • roll-over
    • logical operators
    • type casting
  • We also looked at cloning BitBucket repositories onto Lab46

Week 1

  • Welcome! Be sure to:
    • Read over the syllabus
    • Subscribe to the class mailing list
    • Using the tutorial, set up a screen session and get on #csci on irc
  • Get familiar with logging into the pod systems, and once there:
    • opening up a terminal
    • logging that terminal onto Lab46 for class work and attendance
  • Get familiar with how to log onto Lab46, and once on:
    • change to your src/ subdirectory
    • create/edit .c files (such as hello.c), and how to save/exit
    • compile the C program (.c file(s)) into an executable with gcc
    • execute the compiled C program (the executable) by specifying a path: ./program_name
  • Familiarize yourself with your Opus, and once there:
    • customize it (title/subtitle)
    • add an introduction
    • create your first week content
  • Contemplate our first set of programs we're going to write:
haas/spring2014/cprog/projects.1393324784.txt.gz · Last modified: 2014/02/25 10:39 by wedge