Starting Ch 4
This commit is contained in:
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ ! -d "bin" ]; then
|
||||
mkdir bin
|
||||
echo "Created bin directory"
|
||||
fi
|
||||
|
||||
gcc -c main.c -std=gnu99 -o bin/main.o -O3
|
||||
|
||||
gcc bin/main.o -std=gnu99 -o bin/a.out -O3
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
const int ARRAY_SIZE = 9;
|
||||
const int bills [] = { 60, 35, 61, 160, 30, 15, 200, 15, 25 };
|
||||
|
||||
printf("Going through bills\n");
|
||||
//
|
||||
// Demonstrating the use of break
|
||||
int bills_over = 0;
|
||||
const int BILL_TOLERANCE_LIMIT = 59;
|
||||
|
||||
for (int i = 0; i != ARRAY_SIZE; ++i)
|
||||
{
|
||||
if (bills[i] > BILL_TOLERANCE_LIMIT)
|
||||
{
|
||||
bills_over++;
|
||||
}
|
||||
|
||||
printf("%d\n", bills[i]);
|
||||
|
||||
if (bills_over > 2)
|
||||
{
|
||||
printf("These bills are killing me\n");
|
||||
|
||||
break;
|
||||
}
|
||||
else if (bills_over == 2)
|
||||
{
|
||||
printf("If I see one more bill above %d...\n", BILL_TOLERANCE_LIMIT);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const int collection_of_numbers[] = { 12, 88, 43, 13, 97, 45, 333, 245, 1093 };
|
||||
|
||||
printf("\nGoing through a collection of numbers\n");
|
||||
|
||||
for (int i = 0; i != ARRAY_SIZE; ++i)
|
||||
{
|
||||
if (collection_of_numbers[i] % 2 == 0)
|
||||
{
|
||||
// skipt even numbers
|
||||
continue;
|
||||
}
|
||||
|
||||
printf("%d ", collection_of_numbers[i]);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ ! -d "bin" ]; then
|
||||
mkdir bin
|
||||
echo "Created bin directory"
|
||||
fi
|
||||
|
||||
gcc -c main.c -std=gnu99 -o bin/main.o -O3
|
||||
|
||||
gcc bin/main.o -std=gnu99 -o bin/a.out -O3
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
/* From what i gathered from the author of the C programming boook is that
|
||||
* gotos are highly discouraged, although they have their uses. Goto is
|
||||
* essentially a stronger version of the break statement. Code can become
|
||||
* more difficult to read as more gotos are used.
|
||||
*/
|
||||
|
||||
int main()
|
||||
{
|
||||
const int ARRAY_SIZE = 9;
|
||||
const int numbers1[] = { 60, 35, 61, 160, 30, 15, 200, 15, 25 };
|
||||
const int numbers2[] = { 3, 2, 50, 4, 88, 3, 29, 91, 39 };
|
||||
|
||||
printf("Going through numbers, trying to find a negative number\n");
|
||||
// Demonstrating the use of goto
|
||||
int negative_product = 0;
|
||||
const int BILL_TOLERANCE_LIMIT = 59;
|
||||
|
||||
for (int i = 0; i != ARRAY_SIZE; ++i)
|
||||
{
|
||||
for (int j = 0; j != ARRAY_SIZE; ++j)
|
||||
{
|
||||
int product = numbers1[i] * numbers2[j];
|
||||
|
||||
if (product < 0)
|
||||
{
|
||||
negative_product = product;
|
||||
|
||||
goto found_negative;
|
||||
}
|
||||
|
||||
if ((j + 1) == ARRAY_SIZE && (i + 1) == ARRAY_SIZE)
|
||||
{
|
||||
goto no_negative_found;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
no_negative_found:
|
||||
printf("Did not find any negative products\n");
|
||||
|
||||
return 0;
|
||||
found_negative:
|
||||
printf("Found a negative product: %d\n", negative_product);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user