void main(void){ unsigned char count; count = 10; // Initialize the loop control variable, (count) while (count > 0){ printf("%d ",count); count = count - 1; //update count and repeat } printf("Lift Off\n"); // When count becomes zero we get out of the loop for (count=10; count > 0; count--){ //count-- is short for count = count-1 printf("%d ",count); } printf("Lift Off\n"); } |
void main(void){ unsigned char number, count, result; for (number = 2; number <= 9; number++){ printf("\nMultiplication Table for %d\n", number); for (count = 1; count <= 10; count++){ result = number*count; printf("%d X %d = %d\n", number, count, result); } } } |
#define true 1 #define false 0 #define N 25 // The Size of the Array void main(void){ unsigned short num, partialsum, count; unsigned short Sum[N], FSum[N]; //Declared arrays to store 16-bit values unsigned char correct; for (num = 0; num < N; num++){ partialsum = 0; // partialsum will hold the running sum as we compute it for (count = num+1; count >= 1; count--){ partialsum += count; } Sum[num] = partialsum; //Note Sum[0] holds Sum(1); Sum[1] holds Sum(2) and so on } for (num = 1; num <= N; num++){ FSum[num-1] = (num * (num + 1))/2; // Note FSum[0] holds FSum(1); FSum[1] holds FSum(2) and so on // The formula is Sum(n) = (n * (n+1))/2 } // Check if the formula and computation agree correct = true; // Assume that they match and change it to false if // there is a mismatch for (num = 0; num < N; num++){ if (Sum[num] != FSum[num]) { correct = false; } } if (correct) { printf("Formula Works"); } else { printf("Formula Wrong"); } } |
#define true 1 #define false 0 #define N 25 // The Size of the Array unsigned short Sum[N], FSum[N]; //Declared arrays to store 16-bit values // These are now Global so all sub-routines including // main can manipulate them //Declare function prototypes here so compiler can check if we are // calling them correctly unsigned short hardway(unsigned char); unsigned short formula(unsigned char); void Check(void); // The main program uses a modular decomposition of the problem into // sub-problems that are implemented as functions that are called int main(void){ unsigned char num; for (num = 0; num < N; num++){ Sum[num] = hardway(num); //Call the hardway function by passing it num // put the returned sum in the corresponding location // in the array Sum } for (num = 1; num <= N; num++){ FSum[num-1] = formula(num); } Check(); } unsigned short hardway(unsigned char number ){ unsigned short sum = 0; // partialsum will hold the // running sum as we compute it unsigned char num, count; for (count = number+1; count >= 1; count--){ sum += count; } return sum; // sum holds the result so return it } unsigned short formula(unsigned char number ){ return (number * (number+1))/2; // formula } void Check(void){ unsigned char correct, num; // Check if the formula and computation agree correct = true; // Assume that they match and change it to false if // there is a mismatch for (num = 0; num < N; num++){ if (Sum[num] != FSum[num]) { correct = false; } } if (correct) { printf("Formula Works"); } else { printf("Formula Wrong"); } } |
const struct countTestCase{ unsigned char Letter; // Letter for which to search unsigned char Buffer[12]; // String in which to search unsigned short CorrectCount; // proper result of Count() }; typedef const struct countTestCase countTestCaseType; countTestCaseType countTests[7]={ { 'o', "Hello World", 2}, { 'b', "Bill Bard ", 0}, { 'V', "Jon Valvano", 1}, { 'a', "Yerraballi ", 2}, { 's', "Mississippi", 4}, { '2', "21212121212", 6}, { '1', "11111111111", 11}}; unsigned short Count(unsigned char letter, unsigned char string[12]){ return 1000; // replace this line with your code } int main(void){ unsigned short i,result; unsigned short errors=0; for (i = 0; i < 7; i++){ result = Count(countTests[i].Letter,countTests[i].Buffer); if (result != countTests[i].CorrectCount){ errors++; printf("i=%d, result=%d\n",i,result); } } if (errors==0){ printf("Program works"); } else { printf("Does not work"); } return 0; } |