User Tools

Site Tools


haas:spring2014:cprog:projects

Corning Community College

CSCS1320 C/C++ Programming

Assignments, Documents, Information, and Projects

Projects

Week 10

  • Questions
  • structs
  • structs in action

Week 9

  • Question and answer week. Some good code examples too.

Week 8

  • As I teach the lab, I wasn't able to give out warning grades. I sent a list to Joe, who promptly did not get to submitting warning grades. However… I was rather unsettled by what I saw:
    • 16 total people in the class
    • 7 are doing fine (~43%)
      • most have only submitted 3 or 4 out of 6 projects
      • most have been very attentive on their Opus
      • most have attended most if not all classes
    • 2 would be doing fine (~12%) if they would keep their Opus up-to-date (at least one entry per week)
      • projects/attendance are actually on the upper end
      • don't forget about the Opus!
    • 7 are not doing fine (~43%)
      • none to 2 out of 6 projects submitted
      • attendance is all over the place (never through all)
      • opus is all over the place (1 entry total through just over 1 entry per week)
    • So, 2+7 = 9; 9/16 = 56%.
      • more than half the class is not passing (this is new to me)
        • I don't seem to be getting enough questions based on the lack of work I'm seeing
        • I know some people aren't spending enough time on class material (how are you going to learn it if you don't play with it)
      • 56% of the class would get an F in the class if grades were to be submitted now
  • We seem to have gotten about a week behind on project completion. Well, for those who have been being active and attentive in class.
    • I've made the next project due in 2 weeks instead of 1 to help you get caught up.
  • Remember:
    • I can answer questions from Joe's side of class
    • Joe can answer questions from my side of class
    • We have tutors, they can help
    • I can answers questions from my side of class
      • I can answer said questions outside of class
      • I can answer said questions inside of/during class
        • Asking questions is good

Week 7

  • Be sure your Opus is up to date with your weekly progress and exploits!
  • loops
    • for
    • while
    • do while
  • functions
    • return type
    • parameters
    • function prototypes

Week 6

  • arrays
    • pointer arithmetic
  • loops
    • for
    • while
    • do while
  • Type in these programs, compile/execute them, and pour through the code. Your task is to:
    • figure out what is going on with each and every step
    • ask questions on things you do not understand
  • Be sure to update your Opus!

Single Dimensional Array of Characters

Arrays are commonly used to simulate strings in C.

1
/*
 * This code should produce a warning on compilation. Fix it.
 */
#include <stdio.h>
 
int main()
{
    int i;
    char input[12];
 
    fprintf(stdout, "Enter an 11-character max string: ");
    fgets(input, 11, stdin);
 
    fprintf(stdout, "There are %d characters in your string\n", strlen(input)+1);
 
    for(i=0; i<=strlen(input); i++)
    {   
        if (input[i] == '\n')
            fprintf(stdout, "input[%d]: '\\n' (%3.3hhu, 0x%.2hhX)\n", i, *(input+i), *(input+i));                                                                                   
        else if (*(input+i) == '\0')
            fprintf(stdout, "input[%d]: '\\0' (%3.3hhu, 0x%.2hhX)\n", i, *(input+i), *(input+i));
        else
            fprintf(stdout, "input[%d]: '%c'  (%3.3hhu, 0x%.2hhX)\n", i, *(input+i), *(input+i), *(input+i));
    }   
    return(0);
}

Single Dimensional Array and Memory

To have a better understanding of arrays, we should note how they are represented in memory. Pay close attention to the output of this program:

1
#include <stdio.h>
 
int main()
{
    int i;
    unsigned short int data[8] = { 255, 256, 49152, 13, 65535, 2600 };
 
    fprintf(stdout, "=======================================================\n");
    fprintf(stdout, "Please enter a valid unsigned short int value: ");
    fscanf(stdin, "%hu", &data[6]);
 
    fprintf(stdout, "Please enter another valid unsigned short int value: ");
    fscanf(stdin, "%hu", (data+7));
 
    fprintf(stdout, "The data array starts at address 0x%X\n\n", &data);
    for(i = 0; i < 8; i++)
    {   
        fprintf(stdout, "*(data+%d) contains: %hu (0x%.4X)\n", i, *(data+i), *(data+i));
        fprintf(stdout, " (data+%d) is at address: 0x%X\n", i, (data+i));
        fprintf(stdout, "     Lower-Order byte at 0x%X contains: 0x%.2hhX\n", ((char *)data+(i*2)+0), *((char *)data+(i*2)+0));
        fprintf(stdout, "     Upper-Order byte at 0x%X contains: 0x%.2hhX\n", ((char *)data+(i*2)+1), *((char *)data+(i*2)+1));
        fprintf(stdout, "\n");                                                            
    }
    fprintf(stdout, "=======================================================\n");
 
    return(0);
}

Command-line Arguments and 2D Array Manipulation

Here we play with a two-dimensional array created by the system, via the command-line arguments provided to main():

1
/*
 * Fun with arrays and loops using command-line arguments
 *
 * Try renaming the executable, and running it with different numbers/lengths of arguments
 */
#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.txt · Last modified: 2014/04/18 10:56 by wedge