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

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

Related

How to fix segmentation fault for while loop in c

I'm working on an assignment and trying to write the code to answer this question:
Write a program that computes the total weight of a cargo. The user has many types of boxes (numbered 1 to n). For each box type, the program asks the user about the weight and quantity. The program thencomputes and prints the total cargo weight.
In the output sample below, the user has three box types. For box type 2, the user enters the sentinel -1 to
indicate they’re done with the input. Your program should print Type 1, 2, 3, etc. as shown in the
output below.
Enter weight (lbs) of Type 1 box: 4
Enter quantity: 2
Enter weight (lbs) of Type 2 box: -1
The total weight is 8 lbs.
When i run this code it runs the first line to input weight but then gives me a segmentation fault and says (core dumped). -1 is the sentinel and even when the enter weight is inside the while loop the result is the same. What am I doing wrong? I'm sorry I'm new to C
#include <stdio.h>
int main()
{
int weight; //weight of boxes
int quantity; //number of boxes
int total_weight; //total weight
int n = 1;
printf("Enter weight (lbs) of Type %d box: ", n);
scanf("%d", weight);
while(weight!=-1) //Iterate loop until w=-1
{
printf("Enter quantity: \n");
scanf("%d", quantity);
total_weight= total_weight + (quantity*weight);
n++;
}
printf("The total weight is %0.2d", total_weight);
return 0;
}
This is not how you use scanf
scanf("%d", weight);
scanf("%d", quantity);
You should pass the address of the variable, not the value of the variable.
That would look like this:
scanf("%d", &weight);
scanf("%d", &quantity);
Your while loop depends on value weight. The value of weight never changes in your loop, so the loop can never exit.
This line:
total_weight= total_weight + (quantity*weight);
uses the value of total_weight, which was never initialized.
You should initialize your variables.
All in all, I think your fixed code should look like:
#include <stdio.h>
int main()
{
int weight = 0; //weight of boxes
int quantity = 0; //number of boxes
int total_weight = 0; //total weight
int n = 1;
while(weight!=-1)
{
printf("Enter weight (lbs) of Type %d box: ", n);
scanf("%d", &weight); // Update weight **inside** the loop
printf("Enter quantity: \n");
scanf("%d", &quantity);
total_weight= total_weight + (quantity*weight);
n++;
}
printf("The total weight is %0.2d", total_weight);
return 0;
}
while(weight!=-1) //Iterate loop until w=-1
{
printf("Enter quantity: \n");
scanf("%d", quantity);
total_weight= total_weight + (quantity*weight);
n++;
}
Problem is in while condition, weight never change it's value so the condition always true so infinite loop.
Fix this statement scanf("%d", &weight);
Your problem is you are trying to assign a value to the pointer of quantity and weight, instead you need to put &quantity and &weight also you do not have another input for the weight, you should also use a do while instead of a while loop. It should look like this
#include <stdio.h>
int main()
{
int weight; //weight of boxes
int quantity; //number of boxes
int total_weight; //total weight
int n = 1;
do
{
printf("Enter weight (lbs) of Type %d box: ", n);
scanf("%d", &weight);
printf("Enter quantity: \n");
scanf("%d", &quantity);
total_weight= total_weight + (quantity*weight);
n++;
}while (weight != -1);
printf("The total weight is %0.2d", total_weight);
return 0;
}

Persistent segmentation fault in student grading array program

Creating a program that first takes in maximum amount of points for four assignments. Then it must take in seven students' scores for those four assignments. The program must then output each of the seven students score over the total available points. I can't figure out this pesky segmentation error. Program compiles and input of the maximum points available for the four assignments works just fine, the segmentation comes when I try to enter the students scores. Any help would be greatly appreciated!
#include<stdio.h>
int main (void)
{
int array[4][8];
int max, rows, cols, count;
printf("Please enter the maximum points available for the four assignment");
printf(" (add a space behind each and return when finished): \n");
scanf("%d %d %d %d", &array[0][0], &array[1][0], &array[2][0], &array[3][0]);
max=array[0][0]+array[1][0]+array[2][0]+array[3][0];
printf("Please enter each students set of scores");
printf(" (return after each individual score): \n");
for(cols=1; cols<8; cols++)
{
for(rows=0; rows<4; rows++)
{
scanf("%d", array[rows][cols]);
}
}
for(count=1; count<8; count++)
{
for(cols=1; cols<8; cols++)
{
printf("The points for student #%d, count");
printf(" (%d / %d)",array[0][cols]+array[1][cols]+array[2][cols]+array[3][cols], max);
printf("\n");
}
}
return 0;
}
One of your quotes is misplaced. Change
printf("The points for student #%d, count");
to
printf("The points for student #%d", count);

Getting the min, max, and ave of the five numbers inputted

I'm trying to work on a program in C that gets 5 input numbers and then store these in an array. After getting the the 5 numbers, I must be getting the min, max and the average of the MINIMUN AND MAXIMUM numbers inputted and not all of the five. So here's the code that I made. When I get the maximum number, it seems to be working fine. But when it come's to the min, it's still same as the maximum and so I'll be getting a different average.
#include <stdio.h>
#include <conio.h>
int main()
{
int num[5];
int counter, min, max=0;
float average, total;
min=num;
for(counter=1; counter<=5; counter++)
{
printf("Enter a number: ");
scanf("%d", &num[5]);
if(num[5]>max)
{
max = num[5];
}
if (num[5]<min)
{
min = num[5];
}
}
total = min + max;
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;
}
Since this is a learning exercise, I wouldn't correct your code, but point out what needs to be fixed:
Arrays in C are indexed from zero, not from one, so the counter should go from 0 to 4, inclusive
min is an int, while num is an array, so the assignment min=num is invalid
scanf should put the data into &num[count], not &num[5]
In the way that you coded your loop you do not need an array at all: you need the last number entered.
total cannot be computed as min+max; you need to keep a running total, updating it on each iteration.

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

calculating min and max of 2-D array in 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?

Resources