implementation of trapezoidal numerical integration in C - c

I'm trying to implement numerical integration using the trapezoidal approximation using this formula :
My problem is I don't get how to implement this correctly. To test I wrote a file with 22050 double values all equal to 2 like :
....................
value =2.0;
for ( index = 0 ; index < 22050;index++){
fwrite(&value,sizeof(double),1,inp2);
}
to keep the question simple, say I want to the integral value of each 100 samples:
X Area integral value
0-100 should be 200
100-200 should be 200
..... ...........
22000-22050 should be 100
to do that I 've wrote a program that should do that but the result that get is 4387950 for 100 samples here is my code :
..............................
// opening the files
double* inputData= NULL;
unsigned int N = 100;
double h= 0.0;
unsigned int index= 0;
FILE* inputFile=NULL;
double value =0.0;
int i =0,j=0;
inputFile = fopen("sinusD","rb");
outputFile=fopen("Trapez","wb+");
if( inputFile==NULL || outputFile==NULL){
printf("Couldn't open the files \n");
return -1;
}
inputData = (double*) malloc(sizeof(double)*N);
h=22050/2;
while((i = fread(inputData,sizeof(double),N,inputFile))==N){
value += inputData[0] +inputData[N];
for(index=1;index<N;index++){
value+=(2*inputData[index]);
}
value *=h;
fprintf(outputFile,"%lf ",value);
value =0;
}
if(i!=0){
value = 0;
i=-i;
printf("i value %i\n", i);
fseek(inputFile,i*sizeof(double),SEEK_END);
fread(inputData,sizeof(double),i,inputFile);
for(index=0;index<-i;index++){
printf("index %d\n",index);
value += inputData[0] +inputData[i];
value+=(2*inputData[index]);
}
value *=h;
fprintf(outputFile,"%lf ",value);
value =0;
}
fclose(inputFile);
fclose(outputFile);
free(inputData);
return 0;}
any idea how to do that ?
UPDATE
while((i = fread(inputData,sizeof(double),N,inputFile))==N){
value = (inputData[0] + inputData[N])/2.0;
for(index=1;index<N;index++){
value+=inputData[index];
}
value *=h;
fprintf(outputFile,"%lf ",value);
printf(" value %lf\n",value);
value =0;
}
I get 199.000 as a result for each segment .

Why you didn't start with something simple. Let's say you have the following data {1,2,3,4,5,6,7,8,9,10} and assume h = 1. This is simple,
#include <stdio.h>
#define SIZE 10
int main()
{
double a[SIZE] = {1,2,3,4,5,6,7,8,9,10}, sum = 0.0, trapz;
int h = 1;
int i = 0;
for ( i; i < SIZE; ++i){
if ( i == 0 || i == SIZE-1 ) // for the first and last elements
sum += a[i]/2;
else
sum += a[i]; // the rest of data
}
trapz = sum*h; // the result
printf("Result: %f \n", trapz);
return 0;
}
This is the result
Result: 49.500000
Double check your work with Matlab:
Y = [1 2 3 4 5 6 7 8 9 10];
Q = trapz(Y)
Q =
49.5000
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Edit: For your question in the comment:
This is the matlab code:
X = 0:pi/100:pi; % --> h = pi/100
Y = sin(X); % get values as many as the size of X
Q = trapz(X,Y);
Q =
1.9998
Now to fulfil same scenario in C, do the following
#include <stdio.h>
#include <math.h>
#define SIZE 101
#define PI 3.14159265358
int main()
{
double X[SIZE], Y[SIZE], incr = 0.0, h = PI/100.0, sum = 0.0, trapz;
int i = 0, k = 0, j = 0;
// Generate samples
for ( i; i < SIZE; ++i)
{
X[i] = incr;
incr += h;
}
// Generate the function Y = sin(X)
for ( k; k < SIZE; ++k)
{
Y[k] = sin(X[k]);
}
// Compute the integral of sin(X) using Trapezoidal numerical integration method
for ( j; j < SIZE; ++j){
if ( j == 0 || j == SIZE-1 ) // for the first and last elements
sum += Y[j]/2;
else
sum += Y[j]; // the rest of data
}
trapz = sum * h; // compute the integral
printf("Result: %f \n", trapz);
return 0;
}
The result is
Result: 1.999836

First, your equation is correct, so that's a good start. However, there are a number of variable declarations that you don't supply in your question, so we're left to guess.
First, let's start with the math. For the integral from 0 to 100 to equal 200 with each value being equal to 2.0 implies that h = 1 but your code seems to use a value of 22050/2 which is probably not really what you want.
The code within the loop should look like this:
double value = (inputData[0] + inputData[N])/2.0;
for(index = 1; index < N; ++index){
value += inputData[index];
}
value *= h;
This will give the integral from 0 to N. If you wish to calculate between two arbitrary values, you will have to modify the code appropriately:
int a = 100; // lower limit
int b = 200; // upper limit
double value = (inputData[a] + inputData[b])/2.0;
for(index = a+1; index < b; ++index){
value += inputData[index];
}
value *= h;
As a full example of use, here's a program to calculate the integral of sin(x) from x=pi/4 to x=pi/2:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define M_PI 3.14159265358979323846
int main()
{
int a = 45; // 45 degrees = pi/4 radians
int b = 90; // 90 degrees = pi/2 radians
double h = M_PI/180; // how far apart are samples?
double *inputData = malloc(360*sizeof(double));
if (inputData == NULL) {
printf("Error: ran out of memory!\n");
exit(1);
}
for (int i=0; i<360; ++i)
inputData[i] = sin(i*h);
double value = (inputData[a] + inputData[b])/2.0;
for (int index = a+1; index < b; ++index)
value += inputData[index];
value *= h;
printf("integral from %d to %d = %f\n", a, b, value);
double expected = 1.0/sqrt(2);
printf("(expected value = %f, error = %f)\n", expected, expected-value);
free(inputData);
}
Output from this program on my machine:
integral from 45 to 90 = 0.707089
(expected value = 0.707107, error = 0.000018)

Related

Converting array to 8-bit codes in c

I have an array of 200 elements. I want to divide this array into 25 parts and find the RMS values ​​of these parts and print 0 if it is less than 20, 1 if it is greater.
Example
signal1[200]
Output
data[8]={0,1,0,0,1,0,1,1)
I find code for RMS calculation
#include <stdio.h>
#include <math.h>
double rms(double* v, int n)
{
int i;
double sum = 0.0;
for (i = 0; i < n; i++)
sum += v[i] * v[i];
return sqrt(sum / n);
}
int main(void)
{
double v[] = {3,-3,7,1,-3,9,8,1,3,2,-6,-4,-1,-6,7,-6,-6,-7,-6,-1,-4,9,-1,-7,9,48,-6,-39,-24,-9,10,-24,10,21,-28,-
39,-21,-18,-8,1,-42,-24,30,-48,43,23,-1,8,-27,-4,47,5,2,-27,-1,13,18,-11,-13,49,-47,39,42,30,-41,-24,-17,18,-
37,22,-40,16,-1,28,22,41,39,-17,20,-31,-47,25,0,-2,41,11,12,36,31,8,-32,-26,39,-48,-1,-34,48,21,0,-3,-9,4,-10,-
9,0,-8,7,7,5,-7,3,0,10,3,6,-1,-1,7,-9,-8,-7,-2,7,6,-9,-10,3,-8,16,13,-21,-7,-49,49,-34,-40,-13,-30,-1,-16,46,42,-
45,24,-23,-8,5,45,-8,49,-20,20,17,4,20,17,-33,-38,50,-33,-47,6,39,17,-31,-13,-4,49,-35,36,15,-12,-31,-7,-2,-
8,2,-6,-2,2,-5,-4,2,-5,7,10,5,-3,2,-8,9,8,7,-5,2,-10,-2,-4,-7,-7};
printf("%f\n", rms(v, sizeof(v) / sizeof(double)));
return 0;
}
can you help me please ?
Simply call the function rms with 25 elements at a time:
int main(void)
{
double v[] = {3,-3,7,1,-3,9,8,1,3,2,-6,-4,-1,-6,7,-6,-6,-7,-6,-1,-4,9,-1,-7,9,48,-6,-39,-24,-9,10,-24,10,21,-28,-
39,-21,-18,-8,1,-42,-24,30,-48,43,23,-1,8,-27,-4,47,5,2,-27,-1,13,18,-11,-13,49,-47,39,42,30,-41,-24,-17,18,-
37,22,-40,16,-1,28,22,41,39,-17,20,-31,-47,25,0,-2,41,11,12,36,31,8,-32,-26,39,-48,-1,-34,48,21,0,-3,-9,4,-10,-
9,0,-8,7,7,5,-7,3,0,10,3,6,-1,-1,7,-9,-8,-7,-2,7,6,-9,-10,3,-8,16,13,-21,-7,-49,49,-34,-40,-13,-30,-1,-16,46,42,-
45,24,-23,-8,5,45,-8,49,-20,20,17,4,20,17,-33,-38,50,-33,-47,6,39,17,-31,-13,-4,49,-35,36,15,-12,-31,-7,-2,-
8,2,-6,-2,2,-5,-4,2,-5,7,10,5,-3,2,-8,9,8,7,-5,2,-10,-2,-4,-7,-7};
int data[8], i;
for (i = 0; i < 8; i++) {
data[i] = rms(v + 25 * i, 25) > 20.0;
}
for (i = 0; i < 8; i++) {
printf(" %d", data[i]);
}
putchar('\n');
return 0;
}
The expression v + i gives you the address of the i:th element of v.

Program in C , working with 3 digits but not working with 5 digits

145 = sum of 1! + 4! + 5!. I need to write a program in C, that finds the 5 digit numbers that have this property.
I have written the code successfully for the 3 digits. I used the same code for 5 digits, but it cant find any number.
I would like to help me with my solution, in order for me to see where am I wrong.
#include <stdio.h>
int factorial(int n);
main() {
int pin[5];
int q = 1;
int w = 0;
int e = 0;
int r = 0;
int t = 0;
int result = 0;
int sum = 0;
for (q = 1; q <= 9; q++) {
for (w = 0; w <= 9; w++) {
for (e = 0; e <= 9; e++) {
for (r = 0; r <= 9; r++) {
for (t = 0; t <= 9; t++) {
pin[0] = q;
pin[1] = w;
pin[2] = e;
pin[3] = r;
pin[4] = t;
int factq = factorial(q);
int factw = factorial(w);
int facte = factorial(e);
int factr = factorial(r);
int factt = factorial(t);
sum = factq + factw + facte + factr + factt;
result = 10000 * q + 1000 * w + 100 * e + 10 * r + t * 1;
if (sum == result)
printf("ok");
}
}
}
}
}
}
int factorial(int n) {
int y;
if (n == 1) {
y = 1;
} else if (n == 0)
y = 0;
else {
y = n * factorial(n - 1);
return y;
}
}
Your factorial function doesn't return a value in all cases:
int factorial (int n) {
int y;
if (n==1) {
y = 1;
}
else
if (n==0)
y = 0;
else {
y = n * factorial(n-1);
return y;
}
}
It only returns a value when it makes a recursive call. The base cases don't return anything. Failing to return a value from a function and then attempting to use that value invokes undefined behavior.
Move the return statement to the bottom of the function so it gets called in all cases. Also the value of 0! is 1, not 0.
int factorial (int n) {
int y;
if (n<=1)
y = 1;
else
y = n * factorial(n-1);
return y;
}
Also, when you find the target value you probably want to print it:
printf("ok: %d\n", result);
dbush's answer is accurate in pointing out why your code didn't work. This is an alternative solution to reduce the amount of calculation done by your program by not re-calculating the factorial of each numeral every step of the way. The way your program currently works, it winds up being around 500,000 calls to the factorial function from your nested loop, and then in turn recursively calls the function on average 4ish times for each call from the nested loop, so that's around 2 million calls to factorial. The more digits you tack on, the faster that number grows and more expensive it gets. To avoid all these recalculations, you can create a Look-up table that stores the factorial of the numerals [0-9] and just looks them up as needed.
You can calculate these values ahead of time and initialize your LUT with these values, but if hypothetically you wanted them to be calculated by the program because this is a programming assignment where you can't cut out such a step, it is still pretty trivial to populate the LUT.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
void populate_lut(uint32_t *lut);
int main(void) {
// lut is an array holding the factorials of numerals 0-9
uint32_t lut[10];
populate_lut(lut);
for (uint8_t q = 1; q <= 9; q++) {
for (uint8_t w = 0; w <= 9; w++) {
for (uint8_t e = 0; e <= 9; e++) {
for (uint8_t r = 0; r <= 9; r++) {
for (uint8_t t = 0; t <= 9; t++) {
// now instead of calculating these factorials, just look them up in the look-up table
uint32_t sum = lut[q] + lut[w] + lut[e] + lut[r] + lut[t];
uint32_t result = 10000 * q + 1000 * w + 100 * e + 10 * r + t * 1;
if (sum == result) {
printf("Solution: %" PRIu32 "\n", result);
}
}
}
}
}
}
}
// populate your lookup table with the factorials of digits 0-9
void populate_lut(uint32_t *lut) {
lut[0] = 1;
lut[1] = 1;
for(uint8_t i = 2; i < 10; ++i) {
lut[i] = lut[i-1] * i;
}
}

Convex hull length in C

Im making a program to calculate convex hull length of 2D points.
On the input there is a number of points n and then the coordinates of each point.
for example:
6
-8 -3
-6 1
-5 -2
-3 1
-3 4
2 18
and output is simply the length of the convex hull.
my code looks like this so far:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct point
{
double x;
double y;
}POINT,VECTOR;
POINT b[1000];
VECTOR normal;
int n;
int upper_lower(int i, VECTOR ab, double c) {
double x, y,result;
y = b[i].y;
x = normal.x*b[i].x;
result = -(x + c) / normal.y;
if (y>result) return 1;
if (y == result) return 0;
else
return -1;
}
int ccw(VECTOR v,VECTOR v2)
{
double cp;
cp = v2.x*v.y - v2.y*v.x;
if (cp == abs(cp)) return 1;
else
return -1;
}
double vector_length(VECTOR v)
{
return sqrt(pow(v.x, 2) + pow(v.y, 2));
}
int cmp_points(const void *p1, const void *p2)
{
const POINT *pt1 = p1;
const POINT *pt2 = p2;
// do primary compare on x
if (pt1->x > pt2->x)
return 1;
if (pt1->x < pt2->x)
return -1;
// pt1->x == pt2->x - do secondary compare on y...
if (pt1->y > pt2->y)
return 1;
if (pt1->y < pt2->y)
return -1;
// pt1 == pt2
return 0;
}
int main()
{
int i,poloha,upper[1000],lower[1000],h=0,d=0;
scanf("%d", &n);
if (n <= 0 && n > 1000) return 0;
for (i = 0; i < n; i++)
{
scanf("%lf %lf", &b[i].x, &b[i].y);
}
qsort(b, n, sizeof(POINT), cmp_points);
//split in half
VECTOR ab;
double c;
ab.x = b[n - 1].x - b[0].x;
ab.y = b[n - 1].y - b[0].y;
normal.x = -ab.y;
normal.y = ab.x;
c = -normal.x*b[0].x - (normal.y*b[0].y);
for (i = 0; i < n; i++)
{
poloha = upper_lower(i,ab,c);
if (poloha == 1) upper[h++] = i;
if (poloha == -1) lower[d++]=i;
if (poloha == 0)
{
upper[h++] = i;
lower[d++] = i;
}
}
int j = 0;
double v, length = 0;
VECTOR v1, v2, v3,v4;
v3.x = 0; v3.y = 0;
//lower part
for (i = 0; ; i++)
{
int in = 0;
if (lower[i + 2] < 0)
{
v1.x = b[lower[i + 1]].x - b[lower[0]].x;
v1.y = b[lower[i + 1]].y - b[lower[0]].y;
v2.x = b[lower[i]].x - b[lower[i + 1]].x;
v2.y = b[lower[i]].y - b[lower[i + 1]].y;
lenght += vector_length(v1);
length += vector_length(v2);
break;
}
v1.x = b[lower[i + 1]].x - b[lower[i]].x;
v1.y = b[lower[i + 1]].y - b[lower[i]].y;
v2.x = b[lower[i + 2]].x - b[lower[i]].x;
v2.y = b[lower[i + 2]].y - b[lower[i]].y;
in = ccw(v1, v2);
if (in == 1)
{
length += vector_length(v1);
v3 = v2;
v4 = v1;
}
if (in == -1)
{
length -= vector_length(v4);
if (v3.x != 0 && v3.y != 0)
{
length += vector_length(v3);
v3.x = 0; v3.y = 0;
}
else
{
length += vector_length(v2);
}
}
}
printf("%.3lf", length);
return 0;
}
the problem is that in the last part where I try to compute the length...I just dont know how to finish it..no matter what I try it never works as I want to. Could you guys give me some advice?
I can't see a standard answer, so here's the algorithm:
Choose a point roughly in the centre of your point cloud. Then sort the points radially, by angle from the centre. The topmost point must be in the convex hull, so define it as having an angle of 0.0 and being first in the list.
Now go though. Put point 2 in the "tentative" hull list. Then check point 3. If the angle P1-P2-P3 is concave (relative to the centre point), remove P2 from the list, if it is convex, keep it. Continue like this, backtracking and removing points if they go concave. You only need two points in your "tentative" list, once you have three, they become definite.
You stop when you go full circle and get back to P1.
There are many known convex hull algorithms, one of the simplest of which is the gift wrapping algorithm. Here's an implementation that fits in your program where the points have been input and sorted:
int j = 0; // after sorting, b[0] is leftmost point, must be on an edge
double length = 0;
VECTOR v1, v2, vv;
v1.x = 0, v1.y = 1; // start by measuring angles from vertical
int iv; // index of next edge point
do // find next egde point by minimum angle
{
double lv1 = vector_length(v1), lv2, lv;
double cv = -1; // minimal possible cosine value
for (i = 0; i < n; ++i) if (i != j)
{ // compute cosine of angle between v1 and (b[j]-->b[i]) = v2
v2.x = b[i].x-b[j].x, v2.y = b[i].y-b[j].y;
double c = (v1.x*v2.x + v1.y*v2.y) / lv1 / (lv2 = vector_length(v2));
if (c > cv) cv = c, iv = i, lv = lv2, vv = v2; // new maximum cosine
}
if (v == -1) break;
// printf("%d:%f,%f-->%d:%f,%f = %f\n", j, b[j], iv, b[iv], lv);
length += lv;
v1 = vv; // found edge is new reference edge
} while (j = iv); // repeat while not at start again
printf("%.3lf\n", length);

What's wrong with my code: wrong value output when I try to calculate Euler's number, e, in C?

I'm trying to approximate Euler's number in C using a loop that terminates when the difference between two successive values of e is less than 0.0000001. The value I get is 2.99.. I tried setting it up so that with each iteration, e will be compared with the previous value of itself (held in e1) and if the difference is greater than 0.0000001 it will add another term 1/(n!). What's the issue? I'm new to programming so any advice/critiquing is appreciated.
#include <stdio.h>
int main()
{
float e1 = 0, e2 = 1;
int n = 1, nfact;
while ((e2 - e1) > 0.0000001)
{
e1 = e2;
nfact = 1;
for (int count = 1; count < n; count++)
{
nfact = n * count;
}
n = n + 1;
e2 = e1 + (1.0 / nfact);
}
printf("Approximated value of e = %f", e2);
return 0;
}
nfact = 1;
for (int count = 1; count < n; count++)
{
nfact = n * count;
}
won´t calculate n!.
nfact gets a completely new value every iteration.
Surely you mean
nfact = 1;
for (int count = 2; count <= n; count++)
{
nfact *= count;
}
This is not how you calculate the factorial of a number:
for (int count = 1; count < n; count++)
{
nfact = n * count;
}
Notice that you always assign to nfact on each iteration to the value of n*count, obliterating the previous value of nfact. This piece of code is equivalent to:
nfact = n*(n-1);
Since that's the last value of count.
I think you wanted this instead:
nfact = n;
for (int count = n-1; count > 0; count--)
{
nfact = nfact * count;
}
This is my code for
estimates the value of the mathematical constant e by using the formula:
e = 1 + 1/1! + 1/2! + 1/3! + ....
#include <stdio.h>
int main(void) {
int n = 0; /* loop counter for accuracy */
int accuracy = 10; /* current n factorial */
int fact = 1; /* degree of accuracy */
double e = 0; /* current estimated value of e */
/* loop until degree of accuracy */
while (n <= accuracy) {
if (n == 0) {
fact *= 1;
} else {
fact *= n;
}
e += 1.0 / fact;
++n;
}
printf("e is %f", e);
return 0;
}

C program- find the lowest student score above, and the highest score below the boundary

I am finishing a program where I read in a bunch of non-negative doubles into an array, then calculate the mean and stand dev of the values. Then the mean plus the stand dev represents getting a B.
I am having trouble with the next part, where I need to find the lowest score from the array of numbers that will give me a B, and then the highest value in the array that did not get a B. I am having so much trouble with this part that any help would be amazing.
I also have to make the program stop when EOF is typed into it, but I can not figure that part out either, so any help with that would also be appreciated. For now I instead just made it work for all positive values and stop when a negative value is introduced, here is my code:
#include <stdio.h>
#include <math.h>
int main () {
int arr[100];
int y, x;
int i;
double mean = 0;
double std = 0;
double this = 0;
i = 0;
printf("Enter next number, EOF to stop > ") ;
scanf("%d",&x);
while (x >= 0) {
arr[i++] = x;
printf ("Enter next number, EOF to stop > " );
scanf("%d",&x);
}
y = i;
double sum = 0;
double sum1= 0;
for(i = 0; i < y; i++){
sum = sum + arr[i];
}
mean = sum / y;
for (i = 0; i < y; i++){
sum1 = sum1 + pow((arr[i] - mean), 2);
}
std = sum1 / ((float)y - 1);
this = mean + sqrt(std);
if (10 > y) {
printf("**You must enter atleast 10 scores***\n");
return 0;
}
printf("Mean = %.2lf, Standard Deviation = %.2lf\n", mean, sqrt(std));
printf("Scores above %.2lf get a B\n", this);
return 0;
}
Code:
#include <stdio.h>
#include <math.h>
int main () {
int arr[100];
int y, x;
int i;
double mean = 0;
double std = 0;
double margin = 0;
i = 0;
printf("Enter next number, EOF to stop > ") ;
scanf("%d",&x);
while (x >= 0) {
arr[i++] = x;
printf ("Enter next number, EOF to stop > " );
scanf("%d",&x);
}
y = i;
if (10 > y) {
printf("**You must enter atleast 10 scores***\n");
return 0;
}
double sum = 0;
double sum1= 0;
for(i = 0; i < y; i++){
sum = sum + arr[i];
}
mean = sum / y;
for (i = 0; i < y; i++){
sum1 = sum1 + pow((arr[i] - mean), 2);
}
std = sum1 / ((float)y - 1.0);
margin = mean + sqrt(std);
printf("Mean = %.2lf, Standard Deviation = %.2lf\n", mean, sqrt(std));
printf("Scores above %.2lf get a B\n", margin);
int below = arr[0]; // highest value in the array that will not get a B
int above = arr[0]; // lowest value in the array that will give a B
for (i=0; i<y; i++) {
if ((arr[i] > below) && (arr[i] < margin)) {
below = arr[i];
}
else if ((arr[i] > margin) && (arr[i] < above)) {
above = arr[i];
}
}
return 0;
}
First of all, if you intend to program in -ansi -pedantic C, all variables must be defined at the top of the block. e.g:
Correct:
func() {
variable v1;
variable v2;
perform_stuff();
}
In correct:
func() {
perform_stuff()
variable v1;
}
and now to your question:
if you wish to hold an array of double values, the array should be of type double and not of the type - int.
to find the lowest number in the array:
There are few possible options for that one, first, you could ask the user to enter the values from lowest to highest and then just reach to the array[0] (first location = lowest value). but if you do not/ can not, always use quicksort: http://www.cquestions.com/2008/01/c-program-for-quick-sort.html
to sort the array from lowest values to the highest and then find the value you wish using a binary search: http://www.cquestions.com/2008/01/c-program-for-binary-search.html
to search for the value you wish to.
or you could also make it work this way in the efficiency of O(N):
int heighest_smaller_than_mean = 0;
int smallest_smaller_than_mean = mean;
for(i = 0; i < mean; i++) {
if(heighest_smaller_than_mean < arr[i])
heighest_smaller_than_mean = arr[i];
if(smallest_smaller_than_mean < arr[i])
smallest_smaller_than_mean = arr[i];
}
Hope I understand you correctly :)
For the second question, do not read in the input using fscan(%d, &x), rather create a character array (for example, char [] str = new char[5];) and scan that with fscan(%s, &str). Then, compare the string to another string containing "EOF" using if (strcmp(str, eofStr) == 0) break. Use atoi to convert the string to an integer.
To find the lowest score with a B, store an integer that saves the lowest number with a B. Set the initial value to the A grade value. Iterate through the loop and compare to a score for each iteration. If the score is lower, but still a B, exchange the current lowest score with this score. Finish the loop and you will have the lowest score with a B. You can do the same thing to get the highest score without a B.
int low = this + sqrt(std);
for (int i = 0; arr[i] > 0; i++) {
if (low > arr[i] && arr[i] >= this) low = arr[i];
}

Resources