Solving C Programming Issues: Read & Calculate Numbers

  • Thread starter Thread starter tandoorichicken
  • Start date Start date
  • Tags Tags
    Issues Program
AI Thread Summary
To read and calculate numbers in C, the user needs to ensure proper input handling and memory allocation. The code provided has issues, such as not using the address operator in `scanf` and incorrect conditional checks in the `g_value` function. Specifically, `scanf("%d", n);` should be `scanf("%d", &n);` and `scanf("%lf", x);` should be `scanf("%lf", &x);`. Additionally, the condition `(-4.0<x<3.0)` is incorrectly structured and should be split into two comparisons. The segmentation fault likely arises from these input handling mistakes and logical errors in the code.
tandoorichicken
Messages
245
Reaction score
0
For a homework assignment I need to be able to read in a string of numbers and display them on the screen, and then use them in some calculations. How would be the best way to go about this?
Here's what I have so far to give you an idea of what I need to do:

int n;
double a[15];
printf("Here is the problem:\n");
printf("Enter the number of elements (<16): ");
scanf("%d", n);
printf("\nEnter %d double f-p numbers:\n", n);

Next the user enters 'n' float-point numbers on the screen, and I need to somehow scan that into a[]. How would I do this?
 
Physics news on Phys.org
Best way of approaching programming issues is to approach them from a programming language independent viewpoint.

Imagine you have a person who can handle only one simple instruction at a time, how would you instruct this person to do the job ?

Write this out.

Only when finished think about the data you need to store and the specific programming approach. If you do this, it will be much clearer exactly where the problem is, and somebody will be able to hint you further.

Regards,
Leo
 
Okay, I think I might have that problem figured out. However, here is part of another program I am having trouble with. Could somebody please check the code to make sure nothing is wrong. The compiler doesn't catch anything, so I know it's probably a logic or common sense error on my part.

#include <stdio.h>
#include <math.h>
void problem(void);
double g_value(double x);

void main(void) {
problem(void);
}

void problem (void) {
double x, g_x;
printf("Here is the problem:\n");
printf("Enter a double f-p number:");
scanf("%lf", x);
g_x = g_value(x);
printf("g(%f) = %e", x, g_x);
return;
}

double g_value(double x) {
double g;
if (x>=3.0) g = x*x*x - x*x - 9.0*x + 9;
if (-4.0<x<3.0) g = x*cos(x);
if (x<=-4.0) g = x + 3;
return g;
}

The program runs down to about "Enter a double f-p number" and then abruptly ends with something called a "Segmentation Fault (core dump)."
Can anyone shed some light on this?
 
I multiplied the values first without the error limit. Got 19.38. rounded it off to 2 significant figures since the given data has 2 significant figures. So = 19. For error I used the above formula. It comes out about 1.48. Now my question is. Should I write the answer as 19±1.5 (rounding 1.48 to 2 significant figures) OR should I write it as 19±1. So in short, should the error have same number of significant figures as the mean value or should it have the same number of decimal places as...
Thread 'A cylinder connected to a hanging mass'
Let's declare that for the cylinder, mass = M = 10 kg Radius = R = 4 m For the wall and the floor, Friction coeff = ##\mu## = 0.5 For the hanging mass, mass = m = 11 kg First, we divide the force according to their respective plane (x and y thing, correct me if I'm wrong) and according to which, cylinder or the hanging mass, they're working on. Force on the hanging mass $$mg - T = ma$$ Force(Cylinder) on y $$N_f + f_w - Mg = 0$$ Force(Cylinder) on x $$T + f_f - N_w = Ma$$ There's also...
Back
Top