C Programming Homework Help – The Best of the Best in the UKThe C programming language is one of the most common languages and the most preferred by the majority of institutions when it comes to introducing learners to programming. Most of the high-level programming languages we have today are a lot...
Programming concepts
#include
#include
typedef struct line{
float m;
float b;
int count;
double *x;
double *y;
} line;
line* new_obj(float slope, float intercept, int c)
{
line *obj_ptr = (line*)malloc(sizeof(line));
obj_ptr->m = slope;
obj_ptr->b = intercept;
obj_ptr->count = c;
obj_ptr->x = (double*)malloc(sizeof(double)*c);
obj_ptr->y = (double*)malloc(sizeof(double)*c);
return obj_ptr;
}
int main()
{
FILE *inp = fopen("a5input.txt", "r");
if(inp == NULL)
{
printf("File cannot be opened. Please check and run again!");
exit(1);
}
float slope, intercept;
int c;
fscanf(inp, "%f %f", &slope, &intercept);
fscanf(inp, "%d", &c);
line *obj_ptr = new_obj(slope, intercept, c);
for(int i = 0; i< c; ++i)
fscanf(inp, "%lf", &obj_ptr->x[i]);
for(int i = 0; i< c; ++i)
obj_ptr->y[i] = obj_ptr->m * obj_ptr->x[i] + obj_ptr->b;
FILE *out = fopen("a5output-mercedes-ramon.txt", "w");
fprintf(out, "Slope = %f \nIntercept = %f\n", obj_ptr->m, obj_ptr->b);
for(int i = 0; icount; ++i)
{
char sign1 = '+', sign2 = '+';
if(obj_ptr->x[i] < 0)
sign1 = '-';
if(obj_ptr->y[i] < 0)
sign2 = '-';
fprintf(out, "X%d = %c%8lf\t\tY%d = %c%8lf\n", i+1, sign1, obj_ptr->x[i], i+1, sign2, obj_ptr->y[i]);
}
fclose(inp);
fclose(out);
free(obj_ptr->x);
free(obj_ptr->y);
free(obj_ptr);
return 0;
}
Slope = 2.000000
Intercept = 5.000000
X1 = +1.000000 Y1 = +7.000000
X2 = +2.000000 Y2 = +9.000000
X3 = +4.000000 Y3 = +13.000000
X4 = +8.000000 Y4 = +21.000000
X5 = +16.000000 Y5 = +37.000000
X6 = +31.000000 Y6 = +67.000000
#include
#include
#include
#include
#include
#include
#define ERROR -249852452
#define EXIT -973845634
#define CLEAR -3596893563
float var[10];
int var_ind = 0;
void clear_storage_variables()
{
for(int i = 0; i< 10; ++i)
var[i] = -1;
}
void store_var(float a)
{
if(var_ind != 10)
var[var_ind++] = a;
else
printf("MEMORY FULL!\n");
}
float recall_var()
{
if(var_ind != 0)
return var[--var_ind];
printf("MEMORY EMPTY!\n");
return CLEAR;
}
float input_operand()
{
char input[10];
printf("Enter a number: ");
scanf("%s", input);
float num;
if(isalpha(input[0]))
{
if(strcmp(input,"pi") == 0)
num= 3.14159;
else if(strcmp(input,"e") == 0)
num= 2.7182;
else if(strcmp(input,"rcl") == 0)
{
num = recall_var();
return num;
}
else if(strcmp(input,"c") == 0)
{
printf("0\n");
return CLEAR;
}
else if(strcmp(input,"ca") == 0)
{
clear_storage_variables();
return CLEAR;
}
else if(strcmp(input,"q") == 0)
return EXIT;
}
else
{
sscanf(input, "%f", &num);
}
return num;
}
char* input_operator()
{
char *op = (char *)malloc(sizeof(char)*5);
printf("Enter the operation to perform: ");
scanf("%s",op);
return op;
}
float compute(float a, float b, char op)
{
switch(op)
{
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': if(b==0)
return ERROR;
return a/b;
case '^': return pow(a,b);
}
return 0;
}
float calc(float a, char *op)
{
if(strcmp(op,"sto")==0)
{
store_var(a);
return CLEAR;
}
if(strcmp(op,"sin")==0)
return sin(a);
if(strcmp(op,"cos")==0)
return cos(a);
if(strcmp(op,"tan")==0)
{
if(a==90)
return ERROR;
return tan(a);
}
if(strcmp(op,"arcsin")==0)
return asin(a);
if(strcmp(op,"arccos")==0)
return acos(a);
if(strcmp(op,"arctan")==0)
return atan(a);
if(strcmp(op,"root")==0)
return sqrt(a);
if(strcmp(op,"abs")==0)
return abs(a);
if(strcmp(op,"inv")==0)
return 1/a;
if(strcmp(op,"log")==0)
{
if(a==0)
return ERROR;
return log(a);
}
if(strcmp(op,"log2")==0)
{
if(a==0)
return ERROR;
return log2(a);
}
return 0;
}
int main()
{
FILE *out = fopen("output.txt", "w");
fprintf(out, "CALCULATOR\n");
clear_storage_variables();
while(1)
{
float a = input_operand();
if(a == CLEAR)
continue;
if(a == EXIT)
break;
if(strcmp(op,"+") == 0 || strcmp(op,"-") == 0 || strcmp(op,"*") == 0 || strcmp(op,"/") == 0 || strcmp(op,"^") == 0)
{
float b = input_operand();
if(b == CLEAR)
continue;
if(b == EXIT)
break;
float ans = compute(a, b, op[0]);
if(ans == ERROR)
{
printf("%6.3f %c\n%6.3f =\nerror\n", a, op[0], b);
fprintf(out, "%6.3f %c\n%6.3f =\nerror\n", a, op[0], b);
}
else
{
printf("%6.3f %c\n%6.3f =\n%6.3f\n", a, op[0], b, ans);
fprintf(out, "%6.3f %c\n%6.3f =\n%6.3f\n", a, op[0], b, ans);
}
}
else
{
float ans = calc(a, op);
if(ans == CLEAR)
continue;
if(ans == ERROR)
{
printf("%s %6.3f =\n%error\n", op, a);
fprintf(out, "%s %6.3f =\n%error\n", op, a);
}
printf("%s %6.3f =\n%6.3f\n", op, a, ans);
fprintf(out, "%s %6.3f =\n%6.3f\n", op, a, ans);
}
}
fclose(out);
return 0;
}
Using C for threading programming
#include
#include
#include
#include
#include
#define MAX_CUSM_NUM 100
using namespace std;
// this is the time of the thread. Managed by the main function.
int ticks = 0;
// calculate number of finished customers.
int finished = 0;
// the served_sem used to control the output finished from race condidition of
// reading and writing
sem_tserved_sem;
// the assist_sem used to control the waking of assistants threads
static sem_tassist_sem;
// the seats_sem used to control the 4 seats
static sem_tseats_sem;
// the cout_sem used to control the output to the stdout
static sem_tcout_sem;
// struct of customer
struct customer{
int cid;
int arrival_time;
int service_time;
};
/**
* This function simulates the serving of the customer. It is called when the
* waiting thread that this worker should be served. It just waits to the
* serving time to end. We can call this "The Assistant thread"
* @param customer_ptrcustomer_ptr the customer to be decided for
* @return NULL
*/
void* customer_serving_thread(void *customer_ptr){
customer *c= (customer*)(customer_ptr);
sem_wait(&cout_sem);
cout<< "Time " << ticks << ": Customer " << c->cid<< " starts" <service_time);
sem_post(&assist_sem); // release the assistant
sem_wait(&cout_sem);
cout<< "Time " << ticks << ": Customer " << c->cid<< " done" <arrival_time != ticks);
sem_wait(&cout_sem);
cout<< "Time " << ticks << ": Customer " << c->cid<< " arrives" <cid<< " leaves" <>cid) {
customer *c = new customer();
c->cid = cid;
input >> c->arrival_time;
input >> c->service_time;
// create a waiting thread with this customer.
pthread_create(&threads[nThread++],NULL,customer_waiting_thread,c);
}
// wait for all threads to be done.
while(finished