C: Assignments Day 1

Today we have three problems for you to tackle.

Warning

In this part of the bootcamp we will only be providing you with a minimal number of example files. You will in a hurry have to learn some emacs, basic git and linux commands. The sidebar on the left contains some basic instruction on these (enough to get you through this part of the bootcamp). The training wheels have been removed!

Problem 1: Solve the Quadratic

We wish to solve the quadratic equations, i.e. given a, b, and c, solve the following equation for x.

\[ax^2+bx+c=0\]

The solution from your high school days is the legendary formula:

\[x = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}\]

We want you to write an application developed in C to solve this equation.

To start you off, we have provided a file solveQuadratic.c in /assignments/C-Day1/. The contents of this file are shown below.

 1// program to solve quadratic equation
 2//        ax^2 + bx + c = 0
 3//
 4// soln: x = -b/2a +/- sqrt(b^2-4ac)/2a
 5//
 6// write a program to take 3 inputs and output the soln
 7// if two real solutions, printf("%.8f, %.8f\n", x1, x2);    
 8// if only one solution, just ouput a single value, i.e. printf("%.8f\n", x1);    
 9// if complex, output a complex number, i.e. printf("%.8f + %.8fi,%.8f - %.8fi\n", x1, im, x2,im);    
10
11#include <stdlib.h>
12#include <stdio.h>
13#include <math.h>
14
15int main(int argc, char **argv) {
16
17  if (argc != 4) {
18    printf("Usage: appName a b c\n");
19    exit(-1);
20  }
21
22  float a = atof(argv[1]);
23  float b = atof(argv[2]);
24  float c = atof(argv[3]);
25
26  float x1 = 0, x2 = 0;
27
28  printf("%.8f, %.8f\n", x1, x2);    
29  
30  return 0;
31}
32

Note

When compiling because you will be using functions from the C math library you will need to include the math library when you compile and link your code, i.e.

gcc solveQuadratic.c -lm

Problem 2: Stress Transformations

To transform stress to a rotated coordinate system one can use the formula shown in the figure. We would ask you to write code that will take as input 4 values sigmaXX, sigmaYY, tauXY, and theta compute the stress in the transformed coordinate system. We would ask you to perform that transformation computation in a function other than main and to complicate things, but demonstrate you understand passing of arrays, pass the input and output stresses to this new function in fixed length arrays.

Theory: Stress transformation

\[\begin{split}\sigma_x' = \sigma_x \cos^2\theta + \sigma_y \sin^2\theta + 2 \tau_{xy} \sin\theta \cos\theta \\ \sigma_y' = \sigma_x \sin^2\theta + \sigma_y \cos^2\theta - 2 \tau_{xy} \sin\theta \cos\theta \\ \tau_{xy}' = ( \sigma_y - \sigma_x ) \sin\theta \cos\theta + \tau_{xy} ( \cos^2\theta - \sin^2\theta )\end{split}\]

To start you off, we have provided a file transformStress.c in /assignments/C-Day1/. You are to modify it to print out the transformed stresses. The contents of this file are shown below.

 1// program to transform stress:
 2//
 3// sigmaX' = sigmaX * cos^2(theta) + sigmaY * sin^2(theta) + 2 * tauXY Sin(theta)Cos(theta)
 4// sigmaY' = sigmaX * sin^2(theta) + sigmaY * cos^2(theta) - 2 * tauXY Sin(theta)Cos(theta)
 5// tauXY' = (sigmaY-sigmaX) * sin(theta)cos(theta) + tauXY(cos^2(theta) - sin^2(theta))
 6//
 7// write a program to take:
 8// 1: 4 inputs: sigmaX, sigmaY, tauXY, theta and output transformed stresses: sigmaX', sigmaY', tauXY'
 9// 2: 3  inputs: sigmaX, sigmaY, tauXY and output transformed stresses: sigmaX', sigmaY', tauXY' for every 10 degrees
10//
11// NOTE: perform the transformation inside a function that cannot be named main
12//
13// Extend to possibly include all outputs for a Mohr circle, were the theta now provided is a deltaTheta
14
15#include <stdlib.h>
16#include <stdio.h>
17#include <math.h>
18
19void transformStress(float *stressIN, float theta, float *stressTransformed);
20
21int main(int argc, char **argv) {
22
23  double sigmaX=0, sigmaY=0, tauXY=0;
24
25  printf("%.4f, %.4f, %.4f\n", sigmaX, sigmaY, tauXY);
26  return 0;
27}
28

Note

If you need something extra work, write to a file the results of this transformation from 0 through 360 degrees in increments of 10

To send you data to a file named results.out, start the application as follows:

./appName 1.0 100.0 0.01 1 > results.out

Problem 3: Compute PI numerically

The figure below shows an method to compute pi by numerical integration. We would like you to implement that computation in a C program. The program should take as input an integer value specyfying the number of intervals.

../_images/pi.png

Computation of pi numerically

To start you off, we have provided a file transformStress.c in /assignments/C-Day1/. You are to modify it to print out the transformed stresses. The contents of this file are shown below.

 1#include <stdlib.h>
 2#include <stdio.h>
 3#include <math.h>
 4
 5
 6int main(int argc, char **argv) {
 7
 8  if (argc != 2) {
 9    printf("Usage: appName numSteps\n");
10    exit(-1);
11  }
12
13  int numSteps = atoi(argv[1]);
14  
15  double pi   = 0;
16  
17  printf("PI = %16.14f, diff(%16.14f)\n",pi, fabs(pi-M_PI));
18  return 0;
19}

Note

You will be using your solution on Day 4 as part of the parallel exercises.