
|
ArraysFirst of all, my solution to the last homework assignment (the 5 functions
that you had to write) is given below. Notice that there are lots of ways you
could write square root and cube root in, but this is the most primitive and
straightforward way and it is what I expected of you guys to be able to write.
Thanks to someone from the afternoon section for pointing out that the original
cube root function I had written didn't take care of negative numbers (that you
pass in). It is now corrected and does so correctly.
#include <stdio.h>
#include "../inc/simpio.h"
#define Epsilon 0.000001
double RaiseRealToPower(double x, int k) {
int i;
double answer = 1;
if (k < 0) {
k = -k;
for (i = 0; i < k; i++) {
answer *= x;
}
return (1/answer);
} else {
for (i = 0; i < k; i++) {
answer *= x;
}
return answer;
}
}
double AbsoluteValue(double x) {
if (x < 0) {
return -x;
} else {
return x;
}
}
bool ApproximatelyEqual(double x, double y) {
double result;
if (AbsoluteValue(x) < AbsoluteValue(y)) {
result = AbsoluteValue(x-y)/AbsoluteValue(x);
} else {
result = AbsoluteValue(x-y)/AbsoluteValue(y);
}
return (result < Epsilon);
}
double sqrt(double x) {
double i;
if (x < 0)
return 0;
for (i = 0; (!ApproximatelyEqual(RaiseRealToPower(i, 2), x)) && i < x; i += 0.000001);
return i;
}
double cbrt(double x) {
double i, cubed = x;
if (x < 0)
cubed = -cubed;
for (i = 0; (!ApproximatelyEqual(RaiseRealToPower(i, 3), cubed)) && i < cubed; i += 0.000001);
if (x < 0)
return -i;
else
return i;
}
int main() {
int i;
double result, k;
for (i = -10; i <= 10; i++) {
result = RaiseRealToPower(2, i);
printf("%d^%d = %f\n", 2, i, result);
}
printf("AbsoluteValue(%f) = %f\n", -5.4, AbsoluteValue(-5.4));
printf("AbsoluteValue(%f) = %f\n", 3.2, AbsoluteValue(3.2));
for (k = -5; k <= 5; k += 1.0) {
result = sqrt(k);
printf("sqrt(%f) = %f\n", k, result);
}
for (k = -5; k <= 5; k += 1.0) {
result = cbrt(k);
printf("cbrt(%f) = %f\n", k, result);
}
}
Arrays are a series of consecutive memory locations that
all have the same name.
To declare an array declare it just like any other variable:
int grades[100];In C the memory allocations are labeled from 0-99. This means that grade[0] is the first element of the array and grade[1] is he second element of the array. The number inside the brackets is called a subscript. Array elements can be initialized at the time of declaration:
int counter[5] = {0,1,2,3,4};
char letters[4] = {'a','b','c','d'};
You do not have to initialize the whole array. If you
initialize the first part of the array the rest of the elements
are set to 0. In a character array the rest of the characters
are set to null ('\0').If you wish to initialize an array after declaration, you
cannot use this last method we discussed. You must use a
loop to initialize the elements:
for (i = 0; i < 5; i++) counter[i] = 0; In the statement:
char word[] = {'H','e','l','l','o','!'};
You don’t have to specify the size of the array, the computer
Will automatically make the size equal to the number of
initialized characters. In this case the size of the array is six
and there is no room reserved for the null. Another shortcut
to initialize this array is to do the following:
char word[] = {"Hello!"}; // 7 elements long
Reasons for using arrays:
Macros are used when you want to define a set number to a
name and make that available to all functions.
#define MAX 10 // MAX is not a variable but is accessible to all functions. Classwork program #1
You are a meteorologist and your job is to find the average
temperature of each month. Your boss, however, wants you
to print out a chart for him that shows him, in addition to the
average temperature of the month, the daily temperatures and
their deviation from the average.
Hint: The deviation is calculated by taking the temperature
and subtracting the average temperature of the month.
Use the macro MAX 30 in your program.
Classwork program #2
You are a judge for the Miss America contest. Your boss is
tired of the corruption that has been happening in previous
contests so he tells you to fix this. Your job is to design a
program that tallies the votes of the judges, prints out the
results, and prints out who the winner is. (hint: when girl
number is 0 print out results and print out winner)
Enter vote by typing in girl number #
...
Girl # # of Votes
3
...
The winner is girl number 3!
Homework assignment
Your homework assignment is to:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||