calculating min and max of 2-D array in c - c

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",&degree[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?

Related

How to find a variable that matches the number of unoccupied dwellings?

I was given an assignment asking me to create a program that reads the number of apartment buildings and the number of people living in each apartment. Then we have to calculate the minimum, maximum and average number of residents + the number of unoccupied buildings/dwellings. The last point of the assignment causes me a problem, as I can't figure out how to implement it. The most I could do is to make the program write which apartment is unoccupied, which is still insufficient. I will need a little help.
#include <stdio.h>
#include <stdlib.h>
int main()
{
//Write an algorithm that reads the number of bytes in the apartment building and the numbers living in each apartment.
//Calculate and write down the average, maximum and minimum number of inhabitants, the number of unoccupied dwellings.
int n,i;
printf("Enter the number of apartments in the apartment building:\n");
scanf("%d",&n);
int p[n],s=0;
for(i=0;i<n;i++)
{
printf("Enter the number of residents in %d. apartment:\n",i+1);
scanf("%d",&p[i]);
s+=p[i];
}
int min=p[0];
for(i=1;i<n;i++)
if(p[i]<min)
{
min=p[i];
}
printf("Minimum population: %d\n",min);
int max=p[0];
for(i=1;i<n;i++)
if(p[i]>max)
{
max=p[i];
}
for(i=0;i<n;i++)
if(p[i]==0)
{
printf("%d. apartment is unoccupied.\n",i+1);
}
printf("Maximum population: %d\n",max);
printf("Average population: %.2f\n",(float)s/n);
printf("Number of unoccupied apartment: %d\n");
return 0;
}
This is straightforward enough. You just need to be able to increment a counter, whose initial value is zero, and report its value at the end.
int empty = 0;
for(i=0;i<n;i++)
if(p[i]==0)
{
empty++;
}
printf("Maximum population: %d\n",max);
printf("Average population: %.2f\n",(float)s/n);
printf("Number of unoccupied apartment: %d\n", empty);

Finding the Factorial of range between 1 to N

trying to write a program that would find the factorial of range of numbers starting from 1 to N,N being the final Number to find the factorial for, i have written a non-recursive program.it only works for integers 1 and 2 in the loop, i'm not sure how to fix it because the logic seems fine,also i'm somewhat still a beginner,so i know i'm probably missing something that's obvious,but in any case here's the code :
#include<stdio.h>
int main() {
int firstnumber;
int finalnumber;
printf("this is a program to calculate the factorial of numbers between 1 to N\n");
printf("please enter the final number : ");
scanf("%d",&finalnumber);
int i;
int factorial=1;
for (firstnumber=1;firstnumber<=finalnumber;firstnumber++) {
printf("the factorial of %d is : ",firstnumber);
for (i=1;i<=firstnumber;i++) {
factorial=factorial*i;
}
printf("%d \n ",factorial);
}
return 0;
}
You need to initialize factorial before each calculation.
int i;
// delete this
//int factorial=1;
for (firstnumber=1;firstnumber<=finalnumber;firstnumber++) {
printf("the factorial of %d is : ",firstnumber);
// move the declaration here
int factorial=1;
for (i=1;i<=firstnumber;i++) {
factorial=factorial*i;
}
printf("%d \n ",factorial);
}
Your two loops are redundant: you can calculate factorial for number from factorial of number - 1
This is the programme with one loop only. Complexity O(N).
Pay attention that you will get overflow rapidly by using int.
#include<stdio.h>
int main() {
int number;
int finalnumber;
printf("this is a program to calculate the factorial of numbers between 1 to N\n");
printf("please enter the final number : ");
scanf("%d",&finalnumber);
int factorial = 1;
for (number=1; number<=finalnumber; number++) {
printf("the factorial of %d is : ",number);
factorial *= number;
printf("%d \n",factorial);
}
return 0;
}

C Program to Predict entry for Linear Least-Squares Fit

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;
}

What is wrong with this code? program that outputs minimum, maximum and average of integers in C using Array

/*C program that outputs minimum, maximum and average of integers*/
#include <stdio.h>
#include <conio.h>
void main()
{
int i,a[5],min,max;
float avg;
printf("\n Enter any number : ");
scanf("%d",&a[0]);
max=a[0];
min=a[0];
avg=0;
for(i=1; i++;)
{
printf("\n Enter any0 Number : ");
scanf("%d",&a[i]);
if(a[i]>max)
{
max=a[i];
}
else
{
min=a[i];
}
avg=avg+a[i];
}
avg=avg/5;
printf("\n The minimum number is %d",min);
printf("\n The maximum number is %d",max);
printf("\n The average is %f",avg);
getch();
}
It should ask the user 5 times but it doesn't :( ?
and the output should be like.. this
Output:
Enter number: 4
Enter number: 6
Enter number: 7
Enter number: 20
Enter number: 1
Minimum is 1.
Maximum is 20.
Average is 7.6.
How can I fix this?
your for loop is wrong
for(i=1; i++;)
this should be
for(i=1;i<5; i++) //because you already scanned a[0].. you need to scan till a[4]
use avg=a[0]; instead of avg=0;
in for loop you need to write use if (a[i]<min) instead of else
Modified code
#include <stdio.h>
void main()
{
int i,a[5],min,max;
float avg;
printf("\n Enter any number : ");
scanf("%d",&a[0]);
max=a[0];
min=a[0];
avg=a[0];
for(i=1;i<5;i++)
{
printf("\n Enter any0 Number : ");
scanf("%d",&a[i]);
if(a[i]>max)
max=a[i];
if( a[i]< min )
min=a[i];
avg=avg+a[i];
}
avg=avg/5;
printf("\n%d ,%d, %f\n",min,max,avg);
}
for(i=1; i++;) should be changed to
for(i=1; i<5; i++)
else { min=a[i]; } should be changed to
if (a[i] < min)
{
min=a[i];
}
You have a wrong for-loop condition as mentioned in other answers.
Furthermore, you should fix the condition where you assign to the min variable in the for-loop. Now it's incorrect.
In addition, you're setting avg = 0 at the beginning which is wrong. It should be avg = a[0], because the average of one element equals the value of that element.

Minmax numbers array

This program gets 5 numbers only from the user then
Store them in an array. Get the min, max, and the average of the numbers inputted. Here's the code I made:
#include <stdio.h>
#include <conio.h>
int main()
{
int num[5];
int min, max=0;
int counter;
float average, total;
max = num[0];
min = num[2];
for(counter=0; counter<=4; counter++)
{
printf("Enter a number: ");
scanf("%d", &num[counter]);
if(num[counter]>max)
{
max = num[counter];
}
if (num[counter]<min)
{
min = num[counter];
}
}
total = max+min;
average = total/2;
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
printf("The average is: %d", average);
getch();
return 0;
}
FInally fixed my error with the min and max and now I'm having trouble with the average. I should only get the average of the min and max numbers but it keeps on showing an average of zero. Can someone help? Thankkyouuu.
//get memory address and store value in it.
void getValue(int *ptr)
{
printf("Enter a number: ");
scanf("%d", ptr);
}
int main()
{
//initialized n=5 as per your requirement. You can even get the value at run time.
int min, max, counter,n=5;
int num[n];
float average,total;
getValue(num); //get num[0]
min=max=total=num[0]; //Initialize min,max,total with num[0]
for(counter=1; counter<n; counter++)
{
getValue(num+counter); //get num[counter]
num[counter]>max?max = num[counter]:max; //find max
num[counter]<min?min = num[counter]:min; //find min
total+=num[counter]; // total = total + num[counter]
}
average = total/n;
printf("The maximum number is: %d\n", max);
printf("The minimum number is: %d\n", min);
printf("The total is: %f\n", total);
printf("The average is: %f\n", average);
return 0;
}
Your calculation of average is wrong; you need to use total/num (remember use float):
total += num[counter];
max and min were incorrectly initialized: num[0], num[2] may be anything when you initialize them.
Aside from your calculation of average being wrong (it's not just total/2), you need to use the correct format specifier in the printf:
printf("The average is: %g", average);
You are using %d which tells printf to expect an integer, but you're giving it a float.
1 Min and max should be initialized
int min = INT_MAX;
int max = INT_MIN;
2 You need to keep a running total of your numbers
int total = 0;
...
// inside loop
scanf("%d", &num[counter]);
total += num[counter];
3 At the end print the average, recommend going to floating point.
printf("The average is: %.1f", (double)total/counter);

Resources