Need to fix a few bugs (preprocessor directives) [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 11 months ago.
Improve this question
task condition: Using the structure type and preprocessor directives, compile a program for input information about N types of computer equipment, which is known: manufacturer, type (printer, scanner, laptop, mouse, keyboard), color, model), get the price by the formula y = 3x ^ 2 + 4x-2, where x is the number of the option plus N. Sort the prices for computer equipment by the method of "bubbles" in ascending order.
#define N 5
#define M 15
#define PRI(X) 3*X*X+4*X-2
typedef struct Ctechnology
{
char firma[M];
char type[M];
int price[N];
} comp;
int main()
{
comp a;
printf("Firm, type, price - (y=3x^2+4x-2)\n ");
for (int i = 0; i < N; i++)
{
a.price[N] = PRI(((i + 1)+N)); // there is a problem
printf("%d) ", i + 1);
scanf("%s %s", a.firma, a.type);
printf("\n | [%d] | Firm %10s | Type %10s | Price %10d |\n", i + 1, a.firma, a.type, a.price[N]);
}
return 0;
}

Running
#define N 5
#define M 15
#define PRI(X) 3*X*X+4*X-2
price[N] = PRI(((i + 1)+N));
through gcc -E (which runs the preprocessor), we get
price[5] = 3*((i + 1)+5)*((i + 1)+5)+4*((i + 1)+5)-2;
In general, you may want to add parentheses around the Xes in your directive, so they're correctly grouped no matter the input:
#define PRI(X) 3*(X)*(X)+4*(X)-2
->
price[5] = 3*(((i + 1)+5))*(((i + 1)+5))+4*(((i + 1)+5))-2;
Then, of course, there's the matter of price[N] always being an out-of-bounds access.

Related

Im trying to code a Discomfort Index program in C but im stuck [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I've built the main part of the program but the task requires us to add a feature in which if the index is above the "No discomfort" zone, the program returns the decrease in temperature required for the index to be at "No discomfort" (considering humidity is consistent).
The problem im facing is I set a variable named x which i want to represent the decrease in temperature needed but when i try to form a equation to solve for x it only prints 0.Im pretty sure i cant give an equation to the compiler to solve but is there any way i can get the decrease needed printed?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float main()
{
float T, RH, x, y;
printf("Insert current temperature in Celsius: \n");
scanf("%f", &T);
printf("Insert current humidity percentage: \n");
scanf("%f", &RH);
float DI = T - 0.55 * (1 - 0.01 * RH) * (T - 14.5);
if (DI < 21)
printf("No discomfort");
else if (DI >= 21 && DI < 24)
printf("Under 50 percent population feels discomfort");
else if (DI >= 24 && DI < 27)
printf("Most 50 percent population feels discomfort");
else if (DI >= 27 && DI < 29)
printf("Most of population suffers discomfort");
else if (DI >= 29 && DI < 32)
printf("Everyone feels severe stress");
else if (DI >= 32)
printf("State of medical emergency");
if (DI >= 21)
DI=21;
x=(DI - 14.5 * 0.55(1 - 0.01 * RH))/(1 - 0.55(1 - 0.01*RH));
printf("\nThe temperature should be decreased to %.2f degrees\n", x);
return 0;
Any help is appreciated
Your compiler is already telling you what's wrong here:
x=(DI - 14.5 * 0.55(1 - 0.01 * RH))/(1 - 0.55(1 - 0.01*RH));
Something like this:
error: called object type 'double' is not a function or function pointer
That's because 0.55(1) is math, not C. You need a *.
Always enable and inspect your compiler warnings before wondering why your code doesn't work.

How can I view a number of user groups? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How can I view a number of user groups?
I mean the implementation in C.
I wanted to use the getgrouplist() function, but I want it to take the number of groups automatically.
Here's an off the cuff program which seems to work on my macOS 10.14.1 system, which seems to be quite behind the times:
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int res = 0;
int ng = 100;
int gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
Here's one which works on Ubuntu 16.04:
#include <stdio.h>
#include <grp.h>
int main(void)
{
int res = 0;
int ng = 100;
gid_t gs[ng];
res = getgrouplist("hacksaw", 20, gs, &ng);
printf("num_grps: %d\nres: %d\n", ng, res);
}
The size 100 was chosen arbitrarily to provide maybe enough space.

Robotics Maze Representation in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
So I'd like to represent a rectangular maze of say dimensions 5x4 (rows x columns) using a 2D array in C language. However I am having trouble specifying what actually needs to be put into the 2D array.
int a[5][4] = {
{},
{},
{},
{},
{},
};
Here is the skeleton of the 2D array, in each row there will be 4 values, I assume that each of these values is a single integer that tells us the properties of a cell in the maze. My problem is, is that really enough? How does a single value tell a robot weather there are 3 walls, 2 walls etc
Someone please enlighten me D:
use specific bits for specific properties of the room
#define ROOM_WALL_ABOVE (1 << 0)
#define ROOM_WALL_LEFT (1 << 1)
#define ROOM_WALL_BELOW (1 << 2)
#define ROOM_WALL_RIGHT (1 << 3)
#define ROOM_DOOR (1 << 4)
int a[5][4] = {0};
a[0][0] = ROOM_WALL_ABOVE | ROOM_WALL_LEFT;
if (a[x][y] & ROOM_WALL_RIGHT) printf("Cannot walk right.\n");
You could use a struct matrix
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
struct walls
{
bool N; // true = wall false = no wall
bool S; // true = wall false = no wall
bool W; // true = wall false = no wall
bool E; // true = wall false = no wall
};
int main()
{
struct walls maze[5][4];
// reset
memset(maze, 0x00, sizeof(maze));
// init
maze[0][0].N = false;
maze[0][0].S = true;
maze[0][0].W = true;
maze[0][0].E = false;
// YOUR STUFF
return 0;
}

Order of evaluation in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The code below gives an answer (further below), that I do not understand.
#include <stdio.h>
int fA (int x) {
int w = x;
printf("%d", x);
if (x > 4)
w += fA(x - 2);
if (x > 2)
w += fA(x - 4);
printf("%d", x);
return w;
}
int fB (int x) {
if (x < 1)
return 1;
int w = x;
if (x > 2)
w = w * fB(x - 1);
if (x > 1)
w= w + fA(x - 1);
return w;
}
int main (void) {
printf("\n %d %d \n", fA(6), fB(3));
return 0;
}
it prints
112264004226 12 11
The question is why?
In my opinion it should starts with 6.
Thanks!
There is no guarantee that parameters to a function will be evaluated in any particular order.
So when you call printf with fA(6) and fB(3) as parameters, the compiler is free to call either one before the other.
In this particular case, fB(3) was evaluated first. But if you use a different compiler, it might evaluate fA(6) first.
There's no defined order. This depends on compiler, for example on:
# gcc -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include- dir=/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin14.5.0
Thread model: posix
this produces
640042261122 12 11
Moreover - the same compiler may decide order to be different for optimization
Is this a puzzle?
I imagine you expect it to start with a 6 because fA(6) comes first. But printf's arguments are being evaluated in reverse order (by my compiler; YMMV), so fB(3) is called first.

About tail recursion [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Recently I am reading Mastering Algorithms with c, and in this book I have 1 exercise that I am not able to implement with c.
Tn = 1 if n=1 ,
Tn = 2T(n/2) + n if n > 1
Anyone can help me? I'll appreciate it a lot.
I have tried.
#include <stdio.h>
int test(int n) {
if (n == 1)
return 1;
else if( n > 1 )
return test(n / 2) * 2 + n;
}
int testtail(int n, int running_result) {
if (n == 1)
return running_result;
else
**return testtail(n / 2, ???? );** // How can I implement the second param
}
I am sorry guys! I am not a native English speaker! Maybe I made some mistakes in grammer! I should apologize for this!

Resources