Table of Contents

Corning Community College

CSCS1320 C/C++ Programming

Assignments, Documents, Information, and Projects

Projects

Week 10

Week 9

Week 8

Week 7

Week 6

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

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

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

Week 2

Week 1