I've been stuck on a problem for awhile, I need to read input from the user in the form,
5 1.2 2.3 3.4 4.5 5.6
where the first integer is the number of floats to expect, and then the floats following are the values I need to store in an array of that size. The code I have that keeps returning an error is,
...
int i = 0, j, k;
float value, *ptr;
// For every element in inputArr...
while (i < inputLength) {
printf("Enter the number of values in this data set, followed by the values: ");
// Get the int value for array creation...
scanf("%d ", &j);
printf("%d", j);
// Save it for the calculations later.
*(lengths + i) = j;
// Create dynamic array of floats.
*(inputArr + i) = calloc(j, sizeof(float));
ptr = *(inputArr + i);
// For the rest of the input read the floats and place them.
k = 0;
while (k < j-1) {
scanf("%f ", &value);
*(ptr + k) = value;
k++;
}
scanf("%f\n", &value);
*(ptr + j - 1) = value;
i++;
}
This throws a segmentation fault when I enter in the input above.
Can someone help me out by telling me what I'm doing incorrectly?
You do not have to include spaces and end-of-strings in your scanf calls.
scanf("%d", &j);
i/o
scanf("%d ", &j);
scanf("%f", &value);
i/o
scanf("%f\n", &value);
etc.
Related
This is the code for finding the numerical solutions of 1D Heat Transfer Equation(well, possibly). The code is getting compiled but the console is not showing any output. The console is staying and not disappearing either. I have tried with getch() and getchar() also but the problem still persists. Please help!
Here's my code:
#include <stdio.h>
#include <math.h>
int
main (int argc, const char *argv[])
{
int a, p, tmax, tmin, l;
int i, j, n, o;
double m, T1, T2, x, al, H[l + 1][tmax + 1];
float k;
printf ("Enter the number of nodes: \n");
scanf ("%d", &a);
printf ("Enter the thickness of the wall: \n");
scanf ("%d", &l);
printf ("Enter the value of maximum time coordinate: \n");
scanf ("%d", &tmax);
printf ("Enter the value of minimum time coordinate: \n");
scanf ("%d", &tmin);
printf ("Enter the number of time steps: \n");
scanf ("%d", &p);
printf ("Enter the value of thermal diffusivity: \n");
scanf ("%f", &k);
printf ("Enter the value of initial temperature: \n");
scanf ("%lf", &T1);
printf ("Enter the values of temperature at both the boundaries: \n");
scanf ("%lf", &T2);
x = (l / (a - 1)); /*change in spacial coordinate*>
m = (tmax - tmin) / (p - 1); /*change in time coordinate*/
al = (k * (m)) / (pow (x, 2)); /*Formula for CFL constant*/
printf (" The vale of the constant is %lf\n", al);
printf ("the value of the constant should be less than 0.5 for the convergence of iterative
formula. \n");
for (o = 0; o <= tmax; o++)
{
H[0][o] = H[l][o] = T2; /*boundary condition*/
}
for (j = 1; j <= (l - 1); j++)
{
H[j][0] = T1; /*initial condition*/
}
if (al <= 0.5)
{
for (n = 0; n <= (tmax - 1); n++)
{
for (i = 1; i <= (l - 1); i++)
{
H[i][n + 1] =
H[i][n] + (al * (H[i + 1][n] - (2 * H[i][n]) + H[i - 1][n])); /* Iterative Formula for finding the temperature at each grid point*/
printf (" The value of Temperature[i,n] at [i,n] is:\n ");
printf ("%lf at %d , %d is", H[i][n], i, n);
printf ("\n");
}
}
}
else
{
printf ("Error! Solution is unstable. \n");
}
return 0;
}
What's wrong with my code?
Also, how can I take a mathematical function as input from the user instead of T2 in the place of the initial temperature? Sometimes, 1D unsteady heat transfer problems has initial temperature in the form of a mathematical function which can be algebric, trigonometric, hyperbolic etc. like in this problem
"Show that the solution of the ∂2θ/∂x2= ∂θ/∂t
satisfying the conditions (i) θ → 0 as t → ∞, (ii) θ = 0
when x = ±a for all values of t > 0, and (iii) θ = x when t = 0 and −a < x < a is
θ(x,t) = 2a/π ∑(n=1) to ∞((-1)^(n-1))(1/n)(sin(nπx/a) * (exp^((-((nπ)^2)* t)/a^2 )))"
So, I need to take a mathematical function as input from the user. Is there any way to do that using the C standard library? I found a way to do it using expression evaluator/parser but is there any other simpler way to solve this problem?
I'm a beginner to C, and am trying to sort user inputted numbers into odd and even arrays. I don't understand why my code isn't working.
Cheers.
This is my code, I don't understand my mistake.
int x[]= {};
int i=0;
int d=0;
int j=0;
int even[12]={};
int odd[12]={};
printf("Enter amount of numbers: "); // asking user for amount of numbers
scanf("%d", &d);
for (j=0; j<d; j++){
printf("Enter number %d: ", i+1); // scanning input into 'x' array
scanf("%d", x[i]);
}
printf("Even numbers: ");
for (i=0; i<d; i++) {
if (x[i] % 2 == 0) { // sorting into even array
even[i]=x[i];
printf("%d \n", even[i]);
}
}
printf("\n Odd numbers: ");
for (i=0; i<d;i++){
if (x[i] % 2 != 0) { // sorting into odd array
odd[i]=x[i];
printf("%d \n", odd[i]);
}
}
This error message keeps coming up:
$ ./main
Enter amount of numbers: 4
Enter number 1: 6
Segmentation fault (core dumped)
int x[]= {}; doesn't work because it would hold no elements. But initializing it with {} doesn't work in C anyway, do this instead:
int x[24] = {0}; // first element explicitely set to 0, the rest default-initialized to 0
You also need to put {0} for even and odd. If it's compiling for you with {} then it's possible that you're compiling it as a C++ program, or perhaps your compiler just tolerates it anyway (but it won't work on every C compiler).
scanf needs the address of the int, so instead of scanf("%d", x[i]); you need scanf("%d", &x[i]);. But i is the wrong iterator for this for (j = 0; j < d; j++) loop. Instead do this:
for (j = 0; j < d; j++) {
printf("Enter number %d: ", j + 1); // scanning input into 'x' array
scanf("%d", &x[j]);
}
Also note that the way you're doing this, half the array will be left at 0. So for instance if I imputted the values 1 through 6, then odd contains the values 1 0 3 0 5 0.
I wanted to create a program to calculate the regression line of some given data, along with the errors, so I can use in my university assignments. This is the following program:
#include <stdio.h>
#include <math.h>
int main(void)
{
int i,n,N;
double x[n],y[n],a=0.0,b=0.0,c=0.0,d=0.0,D,P=0.0,p,Sx,A,B,dA,dB;
printf("Give the number of data you want to input for x : ");
scanf("\n%d", &N);
printf("\nGive the values of x : ");
for (i=1; i<=N; i++);
{
printf("\n Enter x[%d] = ", i);
scanf("%lf", &x[i]);
a+=x[i];
b+=pow(x[i],2);
}
printf("\nGive the values of y : ");
for (i=1; i<=N; i++);
{
printf("\n Enter y[%d] = ", i);
scanf("%lf", &y[i]);
c+=y[i];
}
D=N*b-pow(a,2);
A=(b*c-a*d)/D;
B=(N*d-a*c)/D;
for (i=1; i<=N; i++);
{
d+=x[i]*y[i];
p=y[i]-A-B*x[i];
P+=pow(p,2);
}
Sx=sqrt(P/N-2);
dA=Sx*sqrt(b/D);
dB=Sx*sqrt(N/D);
printf("\n x \t \t \t y");
for (i=1; i<=N; i++);
printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
printf("\nA = %lf\t B = %lf", A,B);
printf("\nThe errors of A & B are dA = %lf and dB = %lf", dA,dB);
printf("\nThe equation of the regression line is y=%lfx+(%lf)", B,A);
return 0;
}
I have two problems.
Despite giving a value to N, the program runs so that I can only give one value for x and one value for y. Why and where is the mistake?
When printing the "Enter x[%d]", it displays x[11] and at the end when printing "x[%d] = %lf\t%lf = y[%d]", it displays x[0]. Again why and where is the mistake?
Thank you for your help!
You are trying to create a dynamic array in C.
To do that, you need to use dynamic memory allocation with malloc and free. So, your code should look something like this:
int N;
double *x;
printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);
x = malloc(sizeof(double) * N);
Then, at the end of your program you need to free the memory:
free(x);
If you don't want to deal with manual memory management (or can't for some reason), you can use a static maximum array size like this:
#define MAX_N_X 100
int main(void) {
int N;
double x[MAX_N_X];
printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);
if (N > MAX_N_X) {
printf("Can't handle that many inputs! Maximum %d\n", MAX_N_X);
return 0;
}
}
You simply missed two parameters to printf.
Yo wrote:
printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
But it should be:
printf("\nx[%d] = %lf\t%lf = y[%d]", i, x[i], y[i], i);
I found the main problem with this program a few days after posting this and the fix would be removing the ; from the for commands, along with some other minor changes. I thought I might add this comment to let you know and now it is working like a charm. The simplest of mistakes fools even the trained eyes. After I found this, I was shocked that no one picked up on this mistake.
#include <stdio.h>
#include <stdlib.h>
int main() {
int i;
int mult;
int n;
int ans;
ans = mult * i;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 0; i<n; i++) {
printf("%d x %d = %d \n", &mult, &i , &ans);
}
return 0;
}
Hi guys, this is a simple code which is supposed to help the user to list the times table for n times. However, i am receiving undefined behaviour and I am quite stumped as to what is wrong with my implementation of my "for" loop.
I am receiving this as my output.
6356744 x 6356748 = 6356736
for n times in my consoles.
I want to ask
Is anything wrong with the logic of my code? (i assume i do have a problem with my code so please do enlighten me)
Would it be better(or even possible) to use pointers to point to the memory addresses of the mentioned variables when i have to change the value of the variables constantly? If yes, how do i go around doing it?
Thanks!
In printf you must provide integers. You are now giving the addresses of integers. So change
printf("%d x %d = %d \n", &mult, &i , &ans);
to
printf("%d x %d = %d \n", mult, i, ans);
and to make the table, replace ans with just mult*i, so:
printf("%d x %d = %d \n", mult, i, mult*i);
You should also check the return value of scanf to check if it has succeeded reading your input:
do {
printf("Please enter a multiple you want to explore.");
} while (scanf("%d", &mult)!=1);
do {
printf("Please enter the number which you would want to multiply this number till.");
} while (scanf("%d", &n)!=1);
The things you see are the values of the variables memory location.
Change your lines inside for loop as below
ans = mult * i;
printf("%d x %d = %d \n", mult, i, ans);
There are some mistakes in your code .
you are using the & operator in print statement which is used to print the address of the variable.
Initiate the loop with the value '1' instead of '0' & execute the loop till 'i' less than equal to 'n'.
instead of using the ans variable outside the loop , use it inside the loop as it evaluate the multiplication result in each iteration of the loop.
#include <stdio.h>
int main()
{
int i;
int mult;
int n;
int ans;
printf("Please enter a multiple you want to explore.");
scanf("%d", &mult);
printf("Please enter the number which you would want to multiply this number till.");
scanf("%d", &n);
for(i = 1; i<=n; i++) {
ans = mult*i ;
printf("%d x %d = %d \n", mult, i , ans);
}
return 0;
}
Observe the following code. I want to divide the amount by the value of the last element in the array. I have tried in the following way but it is not working. Can anyone tell me what is the proper way to do it?
#include<stdio.h>
int main()
{
int i, j, k, noteNumber, array[100], amount, result;
printf("Enter the number of the notes: \n");
scanf("%d", ¬eNumber);
printf("Enter the value of %d notes: \n", noteNumber);
for(i = 0; i < noteNumber; i++){
scanf("%d", &array[i]);
}
printf("Enter the amount: \n");
scanf("%d", &amount);
i = j;
if(amount / array[j] == 0){
printf("Minimum %d number of is needed", (amount/array[j]));
printf("The value of each note is %d", array[j]);
}
}
As I can see
i = j;
is wrong as you're using the value of an uninitialized variable to assign to another. This does not make any sense and can lead to undefined behavior.
C arrays use 0-based indexing, so for an array of size n, the last index would be n-1.
That said, never use an unbound index for a statically defined array, always perform the bound checking before using the index.
If noteNumber is the size of the array, then the last Element will be
array[noteNumber - 1]
As far as I can see, j isn't even initialized?
You are having a line
i = j;
j is not even initialized so you are doing a mistake here , maybe what
you wanted was
j = i - 1
As i would have incremented to noteNumber in your for loop and array with number of elements n has last element index n-1 because index starts from 0 rather than 1.
So Proper Code Would Be
#include<stdio.h>
int main(){
int i, j, k, noteNumber, array[100], amount, result;
printf("Enter the number of the notes: \n");
scanf("%d", ¬eNumber);
printf("Enter the value of %d notes: \n", noteNumber);
for(i = 0; i < noteNumber; i++){
scanf("%d", &array[i]);
}
printf("Enter the amount: \n");
scanf("%d", &amount);
j = i - 1; // Line Changed
if(amount / array[j] == 0){
printf("Minimum %d number of is needed", (amount/array[j]));
printf("The value of each note is %d", array[j]);
}
}