From 97a693d996c79fb2a008b19750d8bb45512e01a2 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Mon, 6 Feb 2017 11:26:44 -0500 Subject: Inital commit --- accel.c | 14 ++++ accel_def-macro.c | 16 +++++ accel_var.c | 19 ++++++ accelarray.c | 36 +++++++++++ accelmultiplefunctions.c | 36 +++++++++++ addition.c | 16 +++++ celciustofarenhight.c | 12 ++++ cpp/functions.c | 3 + cpp/stuff1.c | 162 +++++++++++++++++++++++++++++++++++++++++++++++ cpp/whilelooppractice.c | 13 ++++ first_code.c | 8 +++ force.c | 26 ++++++++ force2.c | 25 ++++++++ forsin.c | 18 ++++++ gpa.c | 0 ifgrade.c | 46 ++++++++++++++ linear_spaced.c | 33 ++++++++++ matmult.c | 80 +++++++++++++++++++++++ matrixv.c | 30 +++++++++ mean.c | 26 ++++++++ mile2km.c | 12 ++++ nestedloop.c | 23 +++++++ numbers.c | 31 +++++++++ pointer.c | 16 +++++ prints.c | 8 +++ problem1.c | 14 ++++ sinr_r.c | 55 ++++++++++++++++ sintest.c | 9 +++ sphererevol.c | 14 ++++ switchgrade.c | 42 ++++++++++++ transpose.c | 65 +++++++++++++++++++ 31 files changed, 908 insertions(+) create mode 100644 accel.c create mode 100644 accel_def-macro.c create mode 100644 accel_var.c create mode 100644 accelarray.c create mode 100644 accelmultiplefunctions.c create mode 100644 addition.c create mode 100644 celciustofarenhight.c create mode 100644 cpp/functions.c create mode 100644 cpp/stuff1.c create mode 100644 cpp/whilelooppractice.c create mode 100644 first_code.c create mode 100644 force.c create mode 100644 force2.c create mode 100644 forsin.c create mode 100644 gpa.c create mode 100644 ifgrade.c create mode 100644 linear_spaced.c create mode 100644 matmult.c create mode 100644 matrixv.c create mode 100644 mean.c create mode 100644 mile2km.c create mode 100644 nestedloop.c create mode 100644 numbers.c create mode 100644 pointer.c create mode 100644 prints.c create mode 100644 problem1.c create mode 100644 sinr_r.c create mode 100644 sintest.c create mode 100644 sphererevol.c create mode 100644 switchgrade.c create mode 100644 transpose.c diff --git a/accel.c b/accel.c new file mode 100644 index 0000000..6b61c69 --- /dev/null +++ b/accel.c @@ -0,0 +1,14 @@ +/* Find the acceleration */ + +#include + +int main() +{ + static float FORCE_OF_GRAVITY = 9.81; + int pullforce = 20; + float coeffOfFriction = 0.2; + float weight = 5.0; + /*calculate and display acceleration */ + printf("Acceleration a = %f(m/s^2)\n",(pullforce-coeffOfFriction*weight*FORCE_OF_GRAVITY)/weight); + return 0; +} diff --git a/accel_def-macro.c b/accel_def-macro.c new file mode 100644 index 0000000..83b08cc --- /dev/null +++ b/accel_def-macro.c @@ -0,0 +1,16 @@ +#include + +#define M_G 9.81 + +int main() +{ + /*Variables*/ + double mu = 0.2; + double m = 5.0; + double p = 20.0; + + double a = (p-mu*m*M_G)/m; + + printf("Acceleration a=%f(m/s^2)\n",a); + return 0; + } diff --git a/accel_var.c b/accel_var.c new file mode 100644 index 0000000..522267f --- /dev/null +++ b/accel_var.c @@ -0,0 +1,19 @@ +/* Calculate the acceleation using variables */ + +#include + +int main() +{ + /*declare variables*/ + double mu = 0.2; //friction coefficient + double m = 5.0; //mass + double p = 20.0; //external force + + /*calculate accelearation*/ + double a = (p-mu*m*9.81)/m; + + //Display output + + printf("Acceleration a = %f (m/s^2)\n", a); + return 0; +} diff --git a/accelarray.c b/accelarray.c new file mode 100644 index 0000000..293b841 --- /dev/null +++ b/accelarray.c @@ -0,0 +1,36 @@ +/* File: accelarray.c + Calculate the acceleration using arrays fora ccel and time using 11 points */ + +#include +#include + +#define M_G 9.81 +#define N 11 + +int main() +{ + double a[N],t[N]; + double mu=0.2; + double m = 5.0; + double p; + int i; + + double t0 = 0.0; + double tf = 10.0; + + for(i=0;i +#define M_G 9.81 + +//Calculate the force + +double force(double t) +{ + double p; + p = 4*(t-3)+20; + printf("Froce p=%f(N)\n",p); + return p; +} + +double accel(double t, double mu, double m) +{ + double a,p; + + p = force(t); + a = (p-mu*m*M_G)/m; + return a; +} + +int main() +{ + double a, mu, m, t; + + mu = 0.2; + m = 5; + t = 2; + printf("Acceleration a = %f (m/s^2)\n",accel(t,mu,m)); + return 0; +} diff --git a/addition.c b/addition.c new file mode 100644 index 0000000..fa21bac --- /dev/null +++ b/addition.c @@ -0,0 +1,16 @@ +/*File: addition.c +Doing stuff with functions */ + +#include + +int addition(int a, int b) +{ + return a + b; +} + +int main() +{ + int sum = addition(5,6); + printf("Sum is %i\n",sum); + return 0; +} diff --git a/celciustofarenhight.c b/celciustofarenhight.c new file mode 100644 index 0000000..bde6de6 --- /dev/null +++ b/celciustofarenhight.c @@ -0,0 +1,12 @@ +/* CelsiusToFahreneit */ +#include +#include +int main() +{ + double input; + printf("Give me a number: "); + scanf("%lf", &input); + double answer = (input * (9.0 / 5.0) + 32); + printf("%8.4lf C = %8.4lf F\n\n", input,answer); + return 0; +} diff --git a/cpp/functions.c b/cpp/functions.c new file mode 100644 index 0000000..1cb8d01 --- /dev/null +++ b/cpp/functions.c @@ -0,0 +1,3 @@ +#include +int main () +{} diff --git a/cpp/stuff1.c b/cpp/stuff1.c new file mode 100644 index 0000000..8f0269c --- /dev/null +++ b/cpp/stuff1.c @@ -0,0 +1,162 @@ +#include +#include +#include +#define A 4 +#define B 2 + +int main() +{ + char doagain = 'Y'; + while (doagain=='Y'|| doagain == 'y') + { + float a[A]; + + float w; + float x; + float y; + float z; + + float fw; + float fx; + float fy; + float fz; + + float b[2][B]; + + float m; + float n; + float o; + float p; + + float im; + float in; + float io; + float ip; + + int any; + printf("press 1 to convert F to C, \n press 2 to convert Inchs to Centimeters, \nor press 3 to convert Degrees to Radians."); + scanf("%i",&any); + + if (any < 1 || any > 3) + { + printf("error! try again! \n"); + } + + if (any == 1) + { + /*Changes a given number of celsius to farenhight*/ + printf("Enter first celsius value"); + scanf("%f",&w); + + printf("Enter second celsius value"); + scanf("%f",&x); + + printf("Enter third celsius value"); + scanf("%f",&y); + + printf("Enter fourth celsius value"); + scanf("%f",&z); + + fw = ((9.0 / 5.0)*(w + 32)); + fx = ((9.0 / 5.0)*(x + 32)); + fy = ((9.0 / 5.0)*(y + 32)); + fz = ((9.0 / 5.0)*(z + 32)); + + a[0]=fw; + a[1]=fx; + a[2]=fy; + a[3]=fz; + + printf("\n\n Celsius to Fahrenheit Array \n a[0]= %.2f, a[1]= %.2f, a[2]%.2f,a[3] %.2f\n\n",a[0],a[1],a[2],a[3]); + } + + if (any == 2) + { + /*This (probably) turns an entered number of centimeters to inches*/ + + printf("Enter first value for centimeters"); + scanf("%f",&m); + + printf("Enter second value for centimeters"); + scanf("%f",&n); + + printf("Enter third value for centimeters"); + scanf("%f",&o); + + printf("Enter fourth value for centimeters"); + scanf("%f",&p); + + im = m*2.54; + in = n*2.54; + io = o*2.54; + ip = p*2.54; + + b[0][0]=im; + b[0][1]=in; + b[1][0]=io; + b[1][1]=ip; + + printf("\n Cm to Inches Array \nb[0][0]=%.2f,b[0][1]=%.2f\n,b[1][0]=%.2f,b[1][1]=%.2f\n\n",b[0][0],b[0][1],b[1][0],b[1][1]); + } + + if (any == 3) + { + /*Radiant*/ + float h; + float i; + float j; + float k; + + printf("Enter the first value in degrees"); + scanf("%f",&h); + + printf("Enter the second value in degrees"); + scanf("%f",&i); + + printf("Enter the thind value in degrees"); + scanf("%f",&j); + + printf("Enter the fourth value in degrees"); + scanf("%f",&k); + float degree[4]; + + degree[0] = h; + degree[1] = i; + degree[2] = j; + degree[3] = k; + + float rad[4]; + + rad[0] = h*(0.0175); + rad[1] = i*(0.0175); + rad[2] = j*(0.0175); + rad[3] = k*(0.0175); + + float matrix[6][4]; + int index = 0; + while (index < 4) + { + matrix[0][index] = sin(rad[index]); + matrix[1][index] = cos(rad[index]); + matrix[2][index] = tan(rad[index]); + matrix[3][index] = asin(matrix[0][index]); + matrix[4][index] = acos(matrix[1][index]); + matrix[5][index] = atan(matrix[2][index]); + index = index+1; + } + //Matrix has functions stored in columns + int row = 0; + int col = 0; + //////// 6 6 3 3 3 4 4 4 + printf("\ndegree radian sin cos tan asin acos atan\n"); + while(col < 4) + { + printf("%.4f %.4f %.2f %.2f %.2f %.3f %.3f %.3f\n",degree[col],rad[col],matrix[0][col],matrix[1][col],matrix[2][col],matrix[3][col],matrix[4][col],matrix[5][col]); + col++; + } + } + printf("type Y to doagain or N to quit"); + scanf(" %c",&doagain); + } + return 0; +} diff --git a/cpp/whilelooppractice.c b/cpp/whilelooppractice.c new file mode 100644 index 0000000..f1d1c9b --- /dev/null +++ b/cpp/whilelooppractice.c @@ -0,0 +1,13 @@ +#include +int main() +{printf("Hello World!\n"); +int ear=40; +while (ear>20) +{ + printf("Elephants are pretty cool!\n"); + printf("%i",ear); + ear=ear-1; +} +return 0; +} + diff --git a/first_code.c b/first_code.c new file mode 100644 index 0000000..87be6e7 --- /dev/null +++ b/first_code.c @@ -0,0 +1,8 @@ +/* this is the first code!*/ +#include + +int main() +{ + printf("I love college\n"); + return 1; +} diff --git a/force.c b/force.c new file mode 100644 index 0000000..70d371b --- /dev/null +++ b/force.c @@ -0,0 +1,26 @@ +/*File force.c +Calculate the piecewise time force. The formula depends on wether time t is less or equal to 3 seconds */ +#include +#define M 3 + +int main() +{ + int x = 0; + while(x<10) + { + double p,t; + printf("Useing time %i", x); + t= x; + if(t<=M) + { + p = 20; + } + else + { + p = 4*(t+2); + } + printf("Force p = %f (N)\n",p); + x++; + } + return 0; +} diff --git a/force2.c b/force2.c new file mode 100644 index 0000000..8806617 --- /dev/null +++ b/force2.c @@ -0,0 +1,25 @@ +/*File : force2.c Calculate the peicewise time force. The formulas depend on wether time t is 0<=t<=3 or t>0*/ +#include +#define M 3 + +int main() +{ + + double p, t; + printf("Please input time in seconds: "); + scanf("%lf",&t); + if(0<=t && t<=M){ + p = 20; + printf("Force p = %f (N)\n",p); + } + else if(t>3) + { + p = 4*(t+2); + printf("Force p = %f (N)\n",p); + } + else + { + printf("Invalid input, time is never negitive\n"); + } + return 0; +} diff --git a/forsin.c b/forsin.c new file mode 100644 index 0000000..23e7250 --- /dev/null +++ b/forsin.c @@ -0,0 +1,18 @@ +/* File: forsin.c + calculate the function sinc(x) = sin(x)/x + for x from -10 to 10 with step size 5 */ + +#include +#include + +int main() +{ + int x; + printf("%3c | %5s\n",'x',"sinc(x)"); + printf("-------------\n"); + for(x=-10;x<=10;x+=5) + { + printf("%3i | %6.3f\n",x,sin(x)); + } + return 0; +} diff --git a/gpa.c b/gpa.c new file mode 100644 index 0000000..e69de29 diff --git a/ifgrade.c b/ifgrade.c new file mode 100644 index 0000000..61dbd10 --- /dev/null +++ b/ifgrade.c @@ -0,0 +1,46 @@ +/*File: ifgrade.c +Reads greades A, B, C, D ,F from the screen and prints the corresponding numerical value (4, 3, 2, 1, 0) of the score*/ + +#include + +int main() +{ + char grade; + double score; + printf("Enter a grade [A,B,C,D,F]: "); + scanf(" %c",&grade); + /*Make the character entered upper case if it is lower case*/ + if(grade >= 97) + { + grade = grade-32; + } + if(grade=='A') + { + score = 4.0; + } + else if(grade=='B') + { + score = 3.0; + } + else if(grade=='C') + { + score = 2.0; + } + else if(grade=='D') + { + score = 1.0; + } + else if(grade=='F') + { + score = 0.0; + } + else + { + score = -1; + printf("Invalid grade %c\n",grade); + } + + if(score != -1) + printf("The score for the grade %c is %.2f\n",grade,score); + return 0; +} diff --git a/linear_spaced.c b/linear_spaced.c new file mode 100644 index 0000000..d8dd301 --- /dev/null +++ b/linear_spaced.c @@ -0,0 +1,33 @@ +/* File: linear_spaced.c + Generates linearly spaced atat for an array with N elements from x0 to xf*/ + +#include + +#define N 11 + +int main() +{ + double x[N], + x0 = 0.0, + xf = 5.0; + double j = x0; + + int i; + + printf("%-4c",'c'); + /* generate linearly spaced data for an array with N elements */ + double step = (xf - x0)/((double)N); + for(i=0;i + +#define M 10 +#define N 20 +#define P 30 + +int main() +{ + double a[M][N]; + double b[N][P]; + double c[M][P]; + int i; + int j; + int k; + + int pop1,pop2; + for(pop1=0;pop1 + +#define M 3 +#define N 3 + +int main() +{ + double a[M][N] = {{ 3,5,6}, + { 4,2,1}, + { 0,7,1}}; + double v[N] = {2,1,-2}; + double b[M]; + + int i,j; + + for(i = 0;i + +#define N 8 + +int main() +{ + /*Declare array a with initialization */ + double a[N] = {3.0,21.0,0.0,-3.0,34.0,-14.0,45.0,18.0}; + double sum, + meanval; + + int i; + + sum = 0; + + for(i=0;i + +int main() +{ + float miles = 23.5; + float conversion = 1.6093; + float answer = miles * conversion; + + printf("%f miles is = %f Km\n",miles,answer); + } + diff --git a/nestedloop.c b/nestedloop.c new file mode 100644 index 0000000..d249dca --- /dev/null +++ b/nestedloop.c @@ -0,0 +1,23 @@ +/*File: nestedloop.c + Make a multiplication table useing 2 nested loops + */ + +#include + +int main() +{ + int x, y; + printf("xxx %-3i %-3i %-3i %-3i %-3i %-3i %-3i %-3i %-3i %-3i\n",1,2,3,4,5,6,7,8,9,10); + printf("------------------------------------------\n"); + for(x=1;x<=10;x++) + { + printf("%-3i|",x); + for(y=1;y<=x;y++) + { + printf("%-3i ",x*y); + } + printf("\n"); + } + printf("------------------------------------------\n"); + return 0; +} diff --git a/numbers.c b/numbers.c new file mode 100644 index 0000000..b402d59 --- /dev/null +++ b/numbers.c @@ -0,0 +1,31 @@ +#include + +int main() +{ + float numbers [16] ={ (2*3), /*a*/ + (1/5), /*b*/ + (1/2), /*c*/ + (3/2), /*d*/ + (1/3), /*e*/ + (2/3), /*f*/ + (1.0/5),/*g*/ + (1.0/2),/*h*/ + (3.0/2),/*i*/ + (1.0/3),/*j*/ + (2.3/3),/*k*/ + (1/5.0),/*l*/ + (1/2.0),/*m*/ + (3/2.0),/*n*/ + (1/3.0),/*o*/ + (2/3.0),/*p*/ + }; + int x = 0; + while(x<16) + { + char letter = x+97; + //printf("%c is %f and %.20f\n",letter,numbers[x],numbers[x]); + x++; + } + return 0; +} + diff --git a/pointer.c b/pointer.c new file mode 100644 index 0000000..48c93ca --- /dev/null +++ b/pointer.c @@ -0,0 +1,16 @@ +/* Use a pointer to assign the adderss of a variable */ + +#include + +int main() +{ + int i, *p; + i=10; + p=&i; + printf("The value of i is %d\n",i); + printf("The address of i is %p\n", &i); + char c = 'a'; + printf("Char: %c\n",c); + return 0; + +} diff --git a/prints.c b/prints.c new file mode 100644 index 0000000..cd088af --- /dev/null +++ b/prints.c @@ -0,0 +1,8 @@ +#include + +int main() +{ +// printf("?/?=\n"); + printf("??=\n"); + return 0; +} diff --git a/problem1.c b/problem1.c new file mode 100644 index 0000000..1f85ff0 --- /dev/null +++ b/problem1.c @@ -0,0 +1,14 @@ +/* Find the acceleration */ + +#include + +int main() +{ + static float FORCE_OF_GRAVITY = 9.81; + int pullforce = 10; + float coeffOfFriction = 0.3; + float weight = 6.0; + /*calculate and display acceleration */ + printf("Acceleration a = %f(m/s^2)\n",(pullforce-coeffOfFriction*weight*FORCE_OF_GRAVITY)/weight); + return 0; +} diff --git a/sinr_r.c b/sinr_r.c new file mode 100644 index 0000000..9128dda --- /dev/null +++ b/sinr_r.c @@ -0,0 +1,55 @@ +/* File sinr_r.c + Calculate function sinr(x,y) = sin(sqrt(x*x+y*y))/sqrt(x*x+y*y) + for x from -10 to 10 with step size 10, + and for y from -10 to 10 with step size 10 */ + +#include +#include +#include + +int main() +{ + double x,x0,xf,xstep, + y,y0,yf,ystep, + function, r, + xsq,ysq,sum; + int i,j,nx,ny; + + printf("%10c %10c %10s %10s %10s %10s\n",'x','y',"xsqared","ysquared","x^2+y^2","sinr(x,y)"); + printf("--------------------------------\n"); + /*Initial, final, step size, number of points*/ + x0 = -10.0; + xf = 10.0; + xstep = 10.0; + nx = (xf-x0)/xstep + 1; + y0=-10.0; + yf=10.0; + ystep=10.0; + ny = (yf-y0)/ystep + 1; + + for(i=x0;i<=xf;i+=xstep) + { + x = i; + for(j=y0;j<=yf;j+=ystep) + { + y = j; + xsq = x*x; + ysq = y*y; + sum = xsq + ysq; + printf("%10.4f %10.4f %10.4f %10.4f %10.4f ",x,y,xsq,ysq,sum); + + /*If the sum has a value smaller than elipson, sinr(x,y) = 1 */ + if(fabs(sum) < FLT_EPSILON) + { + function = 1; + } + else + { + function = sin(sum)/sum; + } + printf("%10.4f\n",function); + } + } + + return 0; +} diff --git a/sintest.c b/sintest.c new file mode 100644 index 0000000..aa9d4b0 --- /dev/null +++ b/sintest.c @@ -0,0 +1,9 @@ +#include +#include + +int main() +{ + printf("Sine of 90 degrees is %f",sin(90)); + return 0; + +} diff --git a/sphererevol.c b/sphererevol.c new file mode 100644 index 0000000..ff1bbf8 --- /dev/null +++ b/sphererevol.c @@ -0,0 +1,14 @@ +/*Calculates the volume of a sphere*/ +#include +#include + +int main() +{ + double radius = 5; + float pi = M_PI; + double radiusCubed = pow(radius,3); + float answer = (4*pi*radiusCubed/3); + + printf("The volume of a sphere with radius %f is %f\n",radius,answer); + return 0; + } diff --git a/switchgrade.c b/switchgrade.c new file mode 100644 index 0000000..b22cd66 --- /dev/null +++ b/switchgrade.c @@ -0,0 +1,42 @@ +/*File: switchgrade.c +This program reads a grade from the screen and prints the corresponding numerical value of the score.*/ + +#include + +int main() +{ + char grade; + double score; + + printf("Enter a grade [A,B,C,D,F]: "); + scanf(" %c",&grade); + + //Force all characters to be uppercase + if(grade>=97) + { + grade = grade-32; + } + + switch(grade) + { + case 'A': + score += 1.0; + case 'B': + score += 1.0; + case 'C': + score += 1.0; + case 'D': + score += 1.0; + case 'F': + break; + default: + score = -1; + printf("Invalid grade %c\n",grade); + break; + } + if(score != -1) + { + printf("The score for the grade %c is %.2f\n",grade,score); + } + return 0; +} diff --git a/transpose.c b/transpose.c new file mode 100644 index 0000000..32c769b --- /dev/null +++ b/transpose.c @@ -0,0 +1,65 @@ +/*File: transpose.c The transpose of matrix A is obtained by interchanging the rows and columns. */ +#include + +#define M 10 +#define N 20 + +int main() +{ + + double a[M][N], b[N][M]; + int row,col; + + for(row=0;row