User Tools

Site Tools


Sidebar

projects

wcp1 (due 20240828)
wcp2 (due 20240904)
pct0 (bonus; due 20240905)
pct1 (bonus; due 20240905)
pct2 (due 20240905)
abc0 (due 20240906)
gtf0 (due 20240911)
pct3 (bonus; due 20240911)
wcp3 (due 20240911)
dtr0 (due 20240918)
pct4 (due 20240918)
wcp4 (due 20240918)
pct5 (bonus; due 20240926)
stl0 (due 20240926)
wcp5 (due 20240926)
gfo0 (due 20241002)
pct6 (due 20241002)
stl1 (due 20241002)
wcp6 (due 20241002)
pct7 (bonus; due 20241009)
stl2 (due 20241009)
wcp7 (due 20241009)
bwp1 (bonus; due 20241016)
pct8 (due 20241016)
ptb0 (due 20241016)
wcp8 (due 20241016)
pct9 (bonus; due 20241023)
ptb1 (due 20241023)
wcp9 (due 20241023)
gfo1 (due 20241030)
pctA (due 20241030)
ptb2 (due 20241030)
wcpA (due 20241030)
pctB (bonus; due 20241106)
wcpB (due 20241106)
wus0 (due 20241106)
pctC (due 20241113)
wcpC (due 20241113)
wus1 (due 20241113)
pctD (bonus; due 20241120)
wcpD (bonus; due 20241120)
wus2 (due 20241120)
bwp2 (bonus; due 20241204)
gfo2 (due 20241204)
pctE (bonus; due 20241204)
wcpE (bonus; due 20241204)
EoCE (due 20241216)
haas:fall2024:c4eng:projects:sof0info

sof0 Tips and Tricks

Declaring a variable

To declare a variable, we need a type and a name.

For example, if we are storing test scores ranging from 0 to 100 (ie no negatives), the best fitting data type would be char, specifically, an unsigned char (that would be the type).

    unsigned char scores  = 0;

As I've mentioned, it is a very good documentation practice to also initialize any declared variables to a sane, known starting value. 0 is an excellent choice for most cases.

Receiving input into a variable

To obtain input from the user (via keyboard), we use the fscanf() function, which like fprintf(), utilizes format specifiers.

The format specifier for an unsigned char is %hhu

    fscanf (stdin, "%hhu", &scores);

NOTE: the variable we are reading into needs to be provided, or passed, by its address. We can do that with most variables by prefixing the & symbol.

Verifying input was successful

How do we know if input worked? We could, in our debugging, print it back out using fprintf():

    fprintf (stdout, "scores: %hhu\n", scores);

What does the '\n' do?

The '\n' character is a special character (an escape character) which signifies the newline character (think: enter being pressed).

How do I do math on variables?

Largely the same way you do it in math class, only with a few noted constraints:

  • result is on the left: variable = equation;
  • use the correct math operator symbols defined in C:
    • '+' addition
    • '-' subtraction
    • '*' multiplication
    • '/' integer division (yielding the quotient)
    • '%' integer division (yielding the remainder)
    • parenthesis are available, and should be utilized for purposes of clarity and preserving desired results if your equation would otherwise violate the basic order of operations.

To add 4 to whatever is in our scores variable:

    scores = scores + 4;

How about some more math examples?

Okay, how about taking an average (of four numbers)?

    unsigned char num1  = 0;
    unsigned char num2  = 0;
    unsigned char num3  = 0;
    unsigned char num4  = 0;
    float average       = 0.0;
 
    // prompt for and accept input
    fprintf (stdout, "Enter the first number: ");
    fscanf (stdin, "%hhu", &num1);
 
    fprintf (stdout, "Enter the second number: ");
    fscanf (stdin, "%hhu", &num2);
 
    fprintf (stdout, "Enter the third number: ");
    fscanf (stdin, "%hhu", &num3);
 
    fprintf (stdout, "Enter the fourth number: ");
    fscanf (stdin, "%hhu", &num4);
 
    // display the input values
    fprintf (stdout, "The average of %hhu, %hhu, %hhu, and %hhu is: ", num1, num2, num3, num4);
 
    // calculate the average
    average = (num1 + num2 + num3 + num4) / 4;
 
    // display the average
    fprintf (stdout, "%.2f\n", average);

What about formatting in the printf?

With most types, placing a number between the % and the value will allocate a fixed amount of space on the screen with which to place the output in (if the number takes up less space than the allocated screen space, if it exceeds, it'll overflow it).

A positive value is right-justified.

A negative value is left-justified.

    int value  = 17;
 
    fprintf (stdout, "eight spaces:    >12345678<\n");
    fprintf (stdout, "right justified: >%8d<\n", value);
    fprintf (stdout, "left-justified:  >%-8d<\n", value);
haas/fall2024/c4eng/projects/sof0info.txt · Last modified: 2019/09/06 19:04 by 127.0.0.1