Can anyone help me fix my C code?
I have already made the formula for Linear Least-Squares Fit to fit data entered by the User.
Where I am having issues with having my program read in a series of x values (where number is known advance, but no more than 100 values) until the user enters the sentinel value -100000. Then prints out a table of values for the least-fit line y=mx+b in two tab-separated. I have not learned to use the getch() function yet so I Cannot use the header to perform this.
This is what I have come up with so far (I am adding the entire code):
#include<stdio.h>
#include<math.h>
int main()
{
int x[30],X=0,xx=0,xy=0,y[30],Y=0,n,
bnum,bden,mnum,mden,a[30],N,Yn;
float b=0.0,m=0.0;
int i=0,sent=0;
printf("Enter the number of ordered pairs\n");
scanf("%d",&n);
printf("Enter the ordered pairs\n");
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
printf("\n");
}
printf("The ordered pairs are \n");
for(i=0;i<n;i++)
{
printf("%2d%2d",x[i],y[i]);
printf("\n");
}
for(i=0;i<n;i++)
{
X=X+x[i]; //Sum of values in x array
xx=xx+x[i]*x[i]; //Sum of square of values in x array
xy=xy+x[i]*y[i]; //Sum of product of values of x and y array
Y=Y+y[i]; //Sum of values in y array
}
bnum=(xx*Y)-(X*xy);
bden=(n*xx)-(X*X);
// y intercept
b=bnum/bden;
printf("The Y intercept b = %.2f \n",b);
mnum=(n*xy)-(X*Y);
mden=bden;
//slope
m=mnum/mden;
printf("The slope m = %.2f \n",m);
i=0;
// reading set of x values
while(sent!=-100000)
{
printf("Enter the x values\n");
scanf("%d",&a[i]);
i=i++;
printf("If done enter -100000 or enter another number to continue\n");
scanf("%d",&sent);
}
N=i;
printf("x y\n");
for(i=0;i<N;i++)
{
Yn=(m*a[i])+b;
printf("%3d%3d\n",a[i],Yn);
}
return 0;
}
Related
When I change the type of array from float to double (float array[][] to double array[][]), it doesn't scan the values correctly. All the values become zero. For example, if I enter 5 for input when it's float, it's 5.000000. However, when it's double, every value I enter is scanned as 0.0000000.
#include <stdio.h>
#include <math.h>
int main() {
int limestone, min=0;
//for entering height of array (number of limetstone)
printf("Enter number of limestone: ");
scanf("%d", &limestone);
float array[limestone][3];
//double array[limestone][3]; (the problem)
//for getting inputs
for(int i=0;i<limestone;i++)
{
printf("Enter the %d porosity, hydraulic conductivity (m/s), specific gravity: ", i+1);
scanf("%f %f %f", &array[i][0], &array[i][1], &array[i][2] );
}
//for print array (you can remove it)
for(int i=0;i<limestone;i++)
{
for(int j=0;j<3;j++)
{
printf("%f ",array[i][j]);
}
printf("\n");
}
//Comparing 3rd (Last) Column
for(int i=limestone-1;i>=0;i--)
{
if(array[i][2]<array[min][2])
{
min=i;
}
}
printf("The limestone with the lowest specific gravity is Limestone %d with a specific gravity of %f",min+1,array[min][2]);
return 0;
}
I got your code to work by changing your scanf line to
scanf("%lf %lf %lf", &array[i][0], &array[i][1], &array[i][2] );
All I did was change the %f's to %lfs.
This works for me at least, I think it's doing what you want
I am trying to print all inputted number by the user using the code below but instead of printing all inputted numbers it only print the last number I inputted.
#include<stdio.h>
int display(int n, int a, int b)
{
printf("\n\nOrdered pairs are: ");
for(int j=0;j<n;j++)
{
printf("(%d,%d) ",a,b);
}
return 0;
}
int main()
{
int num,i,j,x,y;
printf("Total number of points: ");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n\nPoint #%d: \n",i+1);
printf("x=");
scanf("%d",&x);
printf("y=");
scanf("%d",&y);
printf("Point #%d: (%d,%d)",i+1,x,y);
}
display(i,x,y);
return 0;
}
You don't have memory for storing more than 2 numbers (x and y) which are over-written during each iteration of the loop.
Perhaps you meant to use arrays, or dynamically allocated memory. This:
int x[100], y[100];
is one way, then you can store up to 100 numbers in each of the two arrays. Use array indexing when accessing.
Actually you are changing the value stored in x and y again and again and so, the previous values get destroyed and only the last value is stored. So you can you arrays (which are more easier to use) or you can even use structure (I would prefer to use arrays).
I'm a newbie!
I'm supposed to get 2 integers from the user, and print the result(sum of all numbers between those two integers).
I also need to make sure that the user typed the right number.
The second number should be bigger than the first one.
And if the condition isn't fulfilled, I have to print "The second number should be bigger than the first one." and get the numbers from the user again until the user types right numbers that meet the condition.
So if I programmed it right, an example of the program would be like this.
Type the first number(integer) : 10
Type the second number(integer) : 1
The second number should be bigger than the first one.
Type the first number(integer) : 1
Type the second number(integer) : 10
Result : 55
End
I think that I have to make two loops, but I can't seem to figure out how.
My English is limited, to help your understanding of this quiz, I'll add my flowchart below.
I tried many different ways I can think of, but nothing seems to work.
This is the code that I ended up with now.
But this doesn't work either.
#include <stdio.h>
void main(void)
{
int a = 0;
int b = 0;
int total_sum = 0;
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
while (a > b) {
printf("The second number should be bigger than the first one.\n");
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
}
while (a <= b) {
total_sum += a;
a++;
}
printf("Result : \n", total_sum);
}
Instead of using loop to sum the numbers, we can use mathematical formula.
Sum of first N integers= N*(N+1)/2
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 0;
int sum;
//Run infinite loop untill a>b
while(1)
{
printf("Type the first number : ");
scanf("%d", &a);
printf("Type the second number : ");
scanf("%d", &b);
if(a>b)
{
printf("The second number should be bigger than the first one.\n");
}
else
{
break;
}
}
//Reduce comlexity of looping
sum=((b*(b+1))-(a*(a-1)))/2;
printf("Result : %d " , sum);
return 0;
}
After corrections your code should run. The community has pointed out many mistakes in your code. Here's an amalgamated solution:
#include <stdio.h>
int main(void)
{
int a = 0;
int b = 0;
int correctInput=0;
int total_sum = 0;
do
{
printf("Type the first number : \n");
scanf("%d", &a);
printf("Type the second number : \n");
scanf("%d", &b);
if(a<b)
correctInput=1;
else
printf("The second number should be bigger than the first one.\n");
}
while (correctInput ==0) ;
while (a <= b) {
total_sum += a;
a++;
}
printf("Result : %d \n" , total_sum);
return 0;
}
Factorials are used frequently in probability problems. The factorial of a positive integer n (written n! and pronounced "n factorial") is equal to the product of the positive integers from 1 to n: n! = 1 x 2 x 3 x x n Write a program that takes as input an integer n and computes n!.
Need some help guys, don't know how to do an arithmetic sum of rows only for the numbers that are divided by 3. Example for a 3x3 matrix , output should be only 3 numbers 15,12,10 . Here is my code i made it to work only calculating the arithmetic sum of rows. Thus finding the middle number of every row.
#include<stdio.h>
#include<conio.h>
int main ()
{
int a[10][10],m,n,i,j,sum;
double am[10];
printf("Enter order of matrix ");
scanf("%d%d",&m,&n);
printf("Enter values of matrix\n");
for(i=1;i<=m;i++)
{
sum=0;
for(j=1;j<=n;j++)
{
printf("Enter value of a[%d][%d] ",i,j);
scanf("%d",&a[i][j]);
if(a[i][j]%3==0)
sum=sum+a[i][j];
}
am[i]=(double)sum/n;
}
for(i=1;i<=m;i++)
{
printf("\nArithmetic Mean of row %d = %f",i,am[i]);
}
getch();
}
Change
for(i=1;i<=m;i++)
to
for(i=0;i<m;i++)
and
for(j=1;j<=n;j++)
to
for(j=0;j<n;j++)
These are done because array indices start from 0 and end at length-1 and not from 1 to length. Also do the same for the last loop. Also,it is better to add a check to see if m and n are more than 10.
This program to calculate sum,min and max of the sum of array elements
Max value is the problem, it is always not true.
void main(void)
{
int degree[3][2];
int min_max[][];
int Max=min_max[0][0];
int Min=min_max[0][0];
int i,j;
int sum=0;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("\n enter degree of student no. %d in subject %d:",i+1,j+1);
scanf("%d",°ree[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("\n Student no. %d degree in subject no. %d is %d",i+1,j+1,degree[i][j]);
}
}
for(i=0;i<3;i++)
{
sum=0;
for(j=0;j<2;j++)
{
sum+=degree[i][j];
}
printf("\n sum of degrees of student no. %d is %d",i+1,sum);
min_max[i][j]=sum;
if(min_max[i][j] <Min)
{
Min=min_max[i][j];
}
else if(min_max[i][j]>Max)
{
Max=min_max[i][j];
}
}
printf("\nThe minimum sum of degrees of student no. %d is %d",i,Min);
printf("\nThe maximum sum of degrees of student no. %d is %d",i,Max);
getch();
}
The problem is that you are initialising Min and Max to min_max[0][0] before assigning any values to min_max, so their content is actually undefined.
Put the assignments Min=min_max[0][0] and Max=min_max[0][0] AFTER the scanf calls.
The lines
printf("\nThe minimum sum of degrees is %d",i,Min);
printf("\nThe maximum sum of degrees is %d",i,Max);
will print the only value of i, not Min or Max. Try this instead:
printf("\nThe minimum sum of degrees for %d is %d",i,Min);
printf("\nThe maximum sum of degrees for %d is %d",i,Max);
The line
min_max[i][j]=sum;
will always have a value of 2 for j because it is outside of the for loop. Also, I'm not clear why you would want to store the partial sum of degrees in the min_max array?