Solving C Programming Issues: Read & Calculate Numbers

In summary, the problem is that the code doesn't seem to be able to handle dividing a number by zero.
  • #1
tandoorichicken
245
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
  • #2
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
 
  • #3
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?
 

FAQ: Solving C Programming Issues: Read & Calculate Numbers

How do I read numbers in C programming?

In C programming, you can use the scanf() function to read numbers from the user. This function takes in a format string and the address of the variable where you want to store the input. For example, if you want to read an integer, you would use scanf("%d", &num) where num is the variable that will store the user input.

How can I calculate numbers in C programming?

C programming has built-in arithmetic operators that you can use to perform calculations on numbers. These include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). You can also use functions from the math library, such as pow() for exponentiation and sqrt() for finding square roots.

What is the difference between scanf() and printf() in C programming?

scanf() is used for reading input from the user, while printf() is used for displaying output to the user. Both functions use format strings to specify the type of data being passed in or displayed.

How do I handle errors when reading and calculating numbers in C programming?

When using scanf(), you should always check its return value to make sure that the input was successfully read. If not, you can handle the error accordingly. When performing calculations, you should also check for potential errors, such as dividing by zero or trying to take the square root of a negative number.

Can you give an example of reading and calculating numbers in C programming?

Sure! Let's say we want to read two integers from the user and calculate their sum. We can do this using the following code:

int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("The sum is %d", sum);

If the user enters 10 and 5, the program will output: The sum is 15

Similar threads

Replies
3
Views
1K
Replies
4
Views
956
Replies
12
Views
1K
Replies
12
Views
2K
Replies
3
Views
910
Back
Top