I need help with finding the minimum value of x but I can't seem to get the code right. Please Help. Noob :sad:

#include<iostream>

using std::cout;
using std::cin;

int main()

{
	int x;          // x is the duration of the call
	double sum;
	int number;
	double average;
	sum = 0;
	number =1;
	int max = 0; 
	int min = 0;

	cout<<"Enter telephone call  " <<  number << ": "; 
	
	while (cin>>x)
	{

		if (x > max) // find longest call
		{
			max = x;
		}

		if (min < x)  // find shortest call ?
		{
			x = min;
		}
		
		sum += x; // sum = sum + 
		number++; // number = number + 1
		
		cout<<"Enter telephone call  " <<  number << ": "; // keeps a space after the colon.

	}// close while

average = sum/--number; //(number-1) I put the -- in front of number because
								// subtract # before dividing.

cout<<"The Longest call in minutes is " << max;
cout<<"\nThe Shortest call in minutes is "<< min;
cout<<"\nThe Number of calls made are "<<number++;
cout<<"\nThe average time of the calls are "<< average;	
cout<<'\n';
	return 0;

} //close main

Dani AI

Generated

A few quick, practical points to make the program robust for computing min, max, count and average. and have already flagged the two usual pitfalls: the min-update logic and how min is initialized. A simple, reliable pattern is to (a) seed min/max safely (either by reading the first value or using numeric limits), (b) keep an explicit count starting at 0, (c) update min/max without overwriting the input variable, and (d) compute the average only if count > 0.

A compact, safe example that demonstrates that pattern:

#include <iostream>
#include <limits>

int main() {
    int x;
    long long sum = 0;
    int count = 0;
    int minVal = std::numeric_limits<int>::max();
    int maxVal = std::numeric_limits<int>::min();

    std::cout << "Enter call durations (end with EOF):\n";
    while (std::cin >> x) {
        if (x < minVal) minVal = x;
        if (x > maxVal) maxVal = x;
        sum += x;
        ++count;
    }

    if (count == 0) {
        std::cout << "No data entered.\n";
    } else {
        double avg = static_cast<double>(sum) / count;
        std::cout << "Longest: " << maxVal << "\n"
                  << "Shortest: " << minVal << "\n"
                  << "Count: " << count << "\n"
                  << "Average: " << avg << "\n";
    }
    return 0;
}

Notes and troubleshooting: use std::numeric_limits when you do not want a special first-read branch. If your input uses a sentinel (for example -1) filter it out before updating stats. Use double cast when dividing to avoid integer truncation, and protect against division by zero. If durations are always positive, initializing minVal from the first valid input is also a tidy option.

I need help with finding the minimum value of x but I can't seem to get the code right. Please Help. Noob :sad:

#include<iostream>

using std::cout;
using std::cin;

int main()

{
	int x;          // x is the duration of the call
	double sum;
	int number;
	double average;
	sum = 0;
	number =1;
	int max = 0; 
	int min = 0;

	cout<<"Enter telephone call  " <<  number << ": "; 
	
	while (cin>>x)
	{

		if (x > max) // find longest call
		{
			max = x;
		}

		if (min < x)  // find shortest call ?
		{
			x = min;
		}
		
		sum += x; // sum = sum + 
		number++; // number = number + 1
		
		cout<<"Enter telephone call  " <<  number << ": "; // keeps a space after the colon.

	}// close while

average = sum/--number; //(number-1) I put the -- in front of number because
								// subtract # before dividing.

cout<<"The Longest call in minutes is " << max;
cout<<"\nThe Shortest call in minutes is "<< min;
cout<<"\nThe Number of calls made are "<<number++;
cout<<"\nThe average time of the calls are "<< average;	
cout<<'\n';
	return 0;

} //close main

Error is in if (min < x) section.

Should be if (x < min)
min = x

Otherwise you overwrite the value of x

Also, setting the min = 0 wont get you any new value for min, since the length of the call is always a positive number

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.