Hello !
How i can find the smallest number, higher than average value. For example in deque I have these numbers: 46 84 25 93 91 25 37 32 64 49 17 81 59 38 79 22 57 17 61 11 (transferred from file Stack.txt to deque.txt). Тhe average of these numbers is 49.4, and the smallest number greater than this value should be 57. I should find it without using arrays.
Тhis is my code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <stdio.h>
using namespace std;
int temp;
int br = 1;
struct delem
{
int key;
delem *next;
} *l = NULL, *r = NULL;
void push_l(int n)
{
delem *p;
p = l;
l = new delem;
l->key = n;
l->next = p;
if (r == NULL)
{
r = l;
}
}
void push_r(int n)
{
delem *p;
p = r;
r = new delem;
r->key = n;
r->next = NULL;
if (l == NULL)
{
l = r;
}
else
p->next = r;
}
int pop_l(int &n)
{
delem *p;
if (l)
{
n = l->key;
p = l;
l = l->next;
if (l== NULL)
r = NULL;
delete p;
return 1;
}
else
return 0;
}
int pop_r(int &n)
{
delem *p;
if (r)
{
n = r->key;
if (l == r)
{
delete r;
l = r = NULL;
}
else
{
p = l;
while (p->next != r)
p = p->next;
//p++;
n = r->key;
p->next = NULL;
delete r;
r = p;
return 1;
}
}
else
return 0;
}
void get(int n)
{
int i, t;
for (i = 1; i < br; i++)
{
pop_l(t);
if (i == n)
{
temp = t;
}
push_r(t);
}
}
void print_deque()
{
for (int i = 1; i<br; i++)
{
get(i);
cout << temp << " ";
}
}
void input_deque()
{
int a;
ifstream i_file;
i_file.open("Stack.txt", ios::in);
if (i_file)
{
while (i_file >> a)
{
push_l(a);
}
ofstream f_file;
f_file.open("deque.txt", ios::out);
while (pop_l(a))
{
f_file << a << " ";
cout << a << " ";
}
f_file.close();
i_file.close();
}
else
cout << "Error while opening input file!" << endl;
}
void find_number()
{
int n, m;
int total = 0;
double min = 1;
double count = 0;
double average = 0;
ifstream deque_file;
deque_file.open("deque.txt", ios::in);
if (deque_file)
{
while (deque_file >> n)
{
push_l(n);
while(pop_l(n))
{
//min = (n<min)?n:min;
total+=n;
count++;
}
}
average = total/count;
cout<<"\nSum: " << total << " Count: " << count << " Average: " << average << endl;
}
else
cout << "Error while opening input file!" << endl;
deque_file.close();
ifstream deque_file2;
deque_file2.open("deque.txt", ios::in);
if(deque_file2)
{
while(deque_file2 >> m)
{
push_l(m);
while(pop_l(m))
{
//min = (m>average)?m:average;
if(m > average)
{
min = m;
}
}
}
cout << "\nMin: " << min << endl;
}
else
cout << "Error while opening input file!" << endl;
deque_file2.close();
}
void dclean()
{
ifstream deque_file;
deque_file.open("deque.txt", ios::trunc);
deque_file.close();
}
int main()
{
input_deque();
print_deque();
cout << endl;
find_number();
dclean();
cout << endl;
system("pause");
return 0;
}