The program is about finding the average temperature of all cities, but the result doesn't make sense (it is wrong)
I have tried splitting the program into functions for better understanding, but that didn't help me much.
#define amount_cities 5
#define amount_temp 3 // temp = temperature
int mintemp(int mas[amount_cities][amount_temp], int *index);
int maxtemp(int mas[amount_cities][amount_temp], int *index);
float avgtemp(int mas[amount_cities][amount_temp]);
int main(){
int arr_temp[amount_cities][amount_temp];
int i, j, ind;
// Set a temperatures for each city
for(i = 0; i < amount_cities; ++i){
printf("Temperature - city %d \n", i);
for(j = 0; j < amount_temp; ++j){
printf("Temperature %d - ", j+1);
scanf("%d", &arr_temp[i][j]);
}
}
for(i = 0; i < amount_cities; ++i){
printf("Temperature - city %ds \n", i);
for(j = 0; j < 3; ++j){
printf(" %d.- %d", j+1, arr_temp[i][j]);
}
printf("\n");
}
printf("Minimal temperature = %d ", mintemp(arr_temp, &ind));
printf("for city %d \n", ind);
printf("Maximal temperature = %d ", maxtemp(arr_temp, &ind));
printf("City %d \n", ind);
printf("Average temperature = %.2f\n", avgtemp(arr_temp));
return 0;
}
int mintemp(int mas[amount_cities][amount_temp], int *index){
int m, n, min_t;
min_t = mas[0][0];
*index = 0;
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
if(mas[m][n] < min_t){
min_t = mas[m][n];
*index = m;
}
}
}
return min_t;
}
int maxtemp(int mas[amount_cities][amount_temp], int *index){
int m, n, max_t;
max_t = mas[0][0];
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
if(mas[m][n] > max_t){
max_t = mas[m][n];
*index = m;
}
}
}
return max_t;
}
float avgtemp(int mas[amount_cities][amount_temp]){
int m, n;
float average_t = 0.0; // Average temperature
for(m = 0; m < amount_cities; ++m){
for(n = 0; n < amount_temp; ++n){
average_t += mas[m][n];
}
}
average_t /= amount_cities*mas[m][n];
return average_t;
}
I expected the output to be , 33.00 but got Average temperature = 30.00 instead.
Related
is there any way I convert this program to using structures? I would like to do that but I have no idea how, but I know its possible. I am just begginer in this soI will be thankful for any kind of help. Thank you
#include <stdio.h>
int main(void)
{
int pole[100];
int x, i, j, q, min[100], max[100], min_cislo, max_cislo, min_pocet = 0, max_pocet = 0;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
if(i==0){
min_cislo = pole[i];
max_cislo = pole[i];
min[min_pocet++] = i;
max[max_pocet++] = i;
continue;
}
if(pole[i] <= min_cislo){
if(pole[i] == min_cislo){
min[min_pocet++] = i;
}else{
min_pocet = 0;
min_cislo = pole[i];
min[min_pocet++] = i;
}
}
if(pole[i] >= max_cislo){
if(pole[i] == max_cislo){
max[max_pocet++] = i;
}else{
max_pocet = 0;
max_cislo = pole[i];
max[max_pocet++] = i;
}
}
}
printf("min cislo bolo: %d, na indexoch:", min_cislo);
for(j = 0; j < min_pocet; j++){
printf(" %d,", min[j]);
}
printf("\n");
printf("max cislo bolo: %d, na indexoch:", max_cislo);
for(q = 0; q < max_pocet; q++){
printf(" %d,", max[q]);
}
printf("\n");
return 0;
}
Okay I figured it your like this because the quest was to save value and position of numbers into struct. see my code:
#include <stdio.h>
struct {
int min_hodnota;
int max_hodnota;
int min_pozicia[100];
int max_pozicia[100];
} bod;
int main(void)
{
int pole[100];
int x, i, j, q , min_pocet = 0, max_pocet = 0;
printf("Napis, kolko cisel chces ulozit do pola :");
scanf("%d",&x);
printf("Vloz %d cisla do pola :\n",x);
for(i=0;i<x;i++){
printf("cislo %d -: ",i);
scanf("%d",&pole[i]);
if(i==0){
bod.min_hodnota = pole[i];
bod.max_hodnota = pole[i];
bod.min_pozicia[min_pocet++] = i;
bod.max_pozicia[max_pocet++] = i;
continue;
}
if(pole[i] <= bod.min_hodnota){
if(pole[i] == bod.min_hodnota){
bod.min_pozicia[min_pocet++] = i;
}else{
min_pocet = 0;
bod.min_hodnota = pole[i];
bod.min_pozicia[min_pocet++] = i;
}
}
if(pole[i] >= bod.max_hodnota){
if(pole[i] == bod.max_hodnota){
bod.max_pozicia[max_pocet++] = i;
}else{
max_pocet = 0;
bod.max_hodnota = pole[i];
bod.max_pozicia[max_pocet++] = i;
}
}
}
printf("min cislo bolo: %d, na indexoch:", bod.min_hodnota);
for(j = 0; j < min_pocet; j++){
printf(" %d,", bod.min_pozicia[j]);
}
printf("\n");
printf("max cislo bolo: %d, na indexoch:", bod.max_hodnota);
for(q = 0; q < max_pocet; q++){
printf(" %d,", bod.max_pozicia[q]);
}
printf("\n");
return 0;
}
I guess you want to build a struct whose fields are the bunch of variables you have defined at the beginning. Well, as pointed by #"L. Scott Johnson", you don't have to. But if you want anyways you jus have to define a struct like this:
struct {
int x;
int i;
int j;
int min[100];
}example;
and just use the struct like this:
example.i=4;
example.min={1,4,5,3,4,8,99,123... };
etc.
I have to write a program in C which, prints numbers from collatz as a triangle.
Like this:
I have tried and
This is my output:
This is my code:
int inputLines, startNum, number;
printf("Lines: ");
scanf("%d",& inputLines);
printf("Start: ");
scanf("%d",& startNum);
for(int i = 0; i < inputLines; i++){
printf("\n");
for(int j = 0; j <= i; j++){
printf("%d ", startNum);
if(startNum % 2 == 0){
startNum = startNum / 2;
}else{
startNum = startNum * 3 + 1;
}
}
}
#include <stdio.h>
int main(void) {
int inputLines = 4;
int start = 19;
for(int i = 0; i < inputLines; i++)
{
int n = start;
for(int j=0; j<i+1; ++j)
{
printf("%d ", n);
n = (n%2)? 3*n+1 : n/2;
}
printf("\n");
}
return 0;
}
in function calcdist() program stops running sometimes.
is it my computer or my code?
The program takes size for a 2d array (a square parking slot) then take the cars, after that finds best slot for our car.
best slot must have the largest distance to the nearest point.
distance is calculating by using manhatten distance.
most of the printf s for finding where the problem is.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define CAPACITY 50
int calcdist(int x, int y, int carnum[][CAPACITY], int size);
int main ()
{
int size, numofcars, x, y, mindist = 0, bestx = 0, besty = 0;
printf("Size: ");
scanf("%d", &size);
int carnum[size][size];
printf("Cars: ");
scanf("%d", &numofcars);
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
carnum[i][j] = 0;
}
}
for(int i = 0; i < numofcars; i++){
printf("Locations: ");
scanf("%d %d", &x, &y);
carnum[x - 1][y - 1] = 1;
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
printf("%d ",carnum[i][j]);
}
printf("\n");
}
printf("carnum[3][0]: %d\n",carnum[3][0]);
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(carnum[i][j] == 0){
printf("hi ");
if(mindist < calcdist(i, j, carnum, size)){
mindist = calcdist(i, j, carnum, size);
bestx = i + 1;
besty = j + 1;
printf("mindist is %d\n", calcdist(i, j, carnum, size));
}
}
}
}
printf("Best Slot Found In: %d %d\n", bestx, besty);
return 0;
}
int calcdist(int x, int y, int carnum[][CAPACITY], int size){
int dist = size * 2;
printf("x is: %d y is : %d\n", x, y);
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
printf("i is: %d j is : %d ", i, j);
if((carnum[i][j] == 1) && (dist > (abs(x - i) + abs(y - j) ))){
dist = abs(x - i) + abs(y - j);
}
printf("i is: %d j is : %d\n", i, j);
}
}
return dist;
}
/****** && (logical and) not & (unary operator) ******/
if((carnum[i][j] == 1) && (dist > (abs(x - i) + abs(y - j) )))
I have two functions, one for calculate the min and the max of an array the seconde one is for calculate the average of this array:
void find_min_max(double *tab, int nb, double *pmin, double *pmax) {
double val_min, val_max;
int i;
val_min = tab[0];
val_max = tab[0];
for (i = 0; i < nb; i++) {
if (tab[i] < val_min) {
val_min = tab[i];
} else
if (tab[i] > val_max) {
val_max = tab[i];
}
}
*pmin = val_min;
*pmax = val_max;
}
double find_average(double *tab, int nb) {
double average, sum;
int i;
sum = 0;
for (i = 0; i < nb; i++) {
sum = sum + tab[i];
}
average = sum / nb;
return average;
}
the array that I want to find the min, the max and the average of is a text file that contains floating point numbers:
int main() {
int i, j;
float tab[100] max, min, avg = 0;
FILE *mydata;
FILE *data_res;
f = fopen("data.txt", "r");
new_f = fopen("data_res.txt", "w");
if (mydata == NULL)
printf("Error");
else {
for (i = 0; i < n; i++)
fscanf(mydata, "%f", &tab[i]);
}
for (i = 0; i < 100; i++) {
for (j = i; j < 5; j++) {
fprintf(data_res, "%f ", tab[j]);
avg = find_average(tab);
find_min_max(tab, nb, &min, &max);
}
fprintf(new_f, "MAX = %lf\n", max);
fprintf(new_f, "MIN = %lf\n", min);
}
}
}
But the problem is it does not calculate the correct max and the correct min
There are multiple problems in your code:
n is not defined, the reading loop does not compile
you should test the return value of fscanf() to detect conversion errors and avoid undefined behavior.
you pass the address of float variables to functions that expect double pointers. You should use double for all values.
it is unclear why you use 1196 for the computing loop
it is unclear why you want to compute the average 5 values at a time.
you do not pass the size of the array in avg = find_average(tab);
you always compute the min and max of the first 5 values in find_min_max(tab, 5, &min, &max);
tab_C and b are not defined.
You must explain what you want the code to do with these values. As posted, it is unclear what your intent is from the reading the code.
To compute the running average, min and max, you should modify your code this way:
// compute the average of 5 elements starting at tab[i]
avg = find_average(tab + i, 5);
find_min_max(tab + i, 5, &min, &max);
Here is a modified version:
#include <stdio.h>
void find_min_max(const double *tab, int nb, double *pmin, double *pmax) {
double val_min, val_max;
val_min = val_max = tab[0];
for (int i = 1; i < nb; i++) {
if (tab[i] < val_min) {
val_min = tab[i];
} else
if (tab[i] > val_max) {
val_max = tab[i];
}
}
*pmin = val_min;
*pmax = val_max;
}
double find_average(const double *tab, int nb) {
double sum = 0.0;
for (int i = 0; i < nb; i++) {
sum += tab[i];
}
return sum / nb;
}
int main() {
int i, j;
double tab[1200], tab_C[1200], max, min, avg = 0, H, L, x = 0.2;
FILE *mydata;
FILE *data_res;
file = fopen("data.txt", "r");
data_res = fopen("data_res.txt", "w");
if (mydata == NULL || data_res == NULL) {
fprintf(stderr, "Cannot open files\n");
return 1;
} else {
for (i = 0; i < 1200; i++)
if (fscanf(mydata, "%lf", &tab[i]) != 1) {
fprintf(stderr, "Input error at index %d\n", i);
return 1;
}
}
for (i = 0; i < 1196; i++) {
//this loop is for taking 5 elements by 5
for (j = i; j < i + 5; j++) {
fprintf(data_res, "%f ", tab[j]);
}
fprintf(data_res, "\n");
avg = find_average(tab + i, 5);
find_min_max(tab + i, 5, &min, &max);
L = avg - ((avg - min) * b);
H = avg + ((max - avg) * b);
fprintf(data_res, "MAX = %f\n", max);
fprintf(data_res, "MIN = %f\n", min);
fprintf(data_res, "the average = %f\n", avg);
fprintf(data_res, "low value = %f\n", L);
fprintf(data_res, "High value = %f\n", H);
}
fclose(mydata);
fclose(data_res);
}
}
In find_min_max, try using this:
if (tab[i] > val_max)
{
val_max = tab[i];
}
instead of
else if (tab[i] > val_max)
{
val_max = tab[i];
}
Hello I'm trying to write a program about subsets and my problem is that whenever I try to run the program, it's automatically saying that "Y is a not subset of X" even though it is a subset. Can someone help me with this? This is my code:
#include<stdio.h>
int main()
{
int i, j, size1, size2, flag;
printf("S U B S E T S\n\n");
printf("Enter number of digits for Array X: ");
scanf("%d", &size1);
int array1[size1];
printf("Enter number of digits for Array Y: ");
scanf("%d", &size2);
int array2[size2];
printf("\n");
printf("Enter %d digits for Array X: ", size1);
for(i = 0; i < size1; i++)
{
scanf("%d", &array1[i]);
}
printf("\n");
printf("Enter %d digits for Array Y: ", size2);
for(i = 0; i < size2; i++)
{
scanf("%d", &array2[i]);
}
printf("\n\n");
for(i = 0; i < size2; i++)
for(j = 0; j < size1; j++)
if(array2[i] == array1[j])
flag++;
if(flag == size1)
{
printf("Array X is a subset of Array Y");
}
else
{
printf("Array X is not a subset of Array Y");
}
return 0;
}
It is working with very minor changes. Just loop from 0 and use < instead of <=.
#include<stdio.h>
int isSubset(int arr1[], int arr2[], int m, int n) {
int i = 0;
int j = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (arr2[i] == arr1[j])
break;
}
if (j == m)
return 0;
}
return 1;
}
int main() {
int arr1[20];
int arr2[20];
int m, n;
printf("How many elements of X you want to store?\n");
scanf("%d", &m);
printf("How many elements of Y you want to store?\n");
scanf("%d", &n);
for (int i = 0; i < m; i++) {
printf("\nEnter %d X values to be stored:\n", m);
scanf("%d", &arr1[i]);
}
for (int j = 0; j < n; j++) {
printf("\nEnter %d Y values to be stored:\n", n);
scanf("%d", &arr2[j]);
}
m = sizeof(arr1) / sizeof(arr1[0]);
n = sizeof(arr2) / sizeof(arr2[0]);
if (isSubset(arr1, arr2, m, n))
printf("Y is a subset of X \n");
else
printf("Y is not a subset of X\n");
getchar();
return 0;
}