Minimum processing cost!!

Hello everyone!! I need some help if possible with my problem given to me from my teacher.I will describe it here:
Assume that processing a list of items costs the sum of differences between the item and the max value in that series. For example if we are given the series: 5 3 2 7, thus, the max of this series is 7. Then, the processing cost of it is

= (7-5) + (7-3) + (7-2) + (7-7)
= 2 + 4 + 5 + 0 = 11.

Question: Write a program that is going to read n rows of m element series and then the program will find the minimum processing cost.

Input specification
In the first line, you will be given two numbers (n and m). Then, in the following n lines, you will be given m integers which are between -20000 and 20000 where n and m are between 1 and 20000
Output specification
Show one integer: Minimum processing cost.
Example:: Input= 3 4 Output= 10
7 1 7 9
4 10 8 8
9 2 6 3

Explanation::In sample input 1, there are 3 rows
with 4 elements. The max of first row is 9 and the processing cost of it 12. The max of the second row is 10 with a total processing cost of 10 (6+0+2+2). Then the third series has the max of 9. And it’s processing cost is 16. So, the second row has the minimum processing cost: 10.

My code is this,but it don't show me the right output::

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int n, m, i, cnt, max1, res1=0, res, max2=0, f, k, r, num;
cin>>n>>m;
int list1[m];
int list2 [n];
for(i=0;i<n;i++)
{
f=0;
cin>>list1[i];
max1=list1[i];
for(cnt=1;cnt<m;cnt++){
cin>>list1[cnt];
if (list1[cnt] >= max1) max1=list1[cnt];
}
for(r=0;r<m;r++)
{
res=max1-list1[r];
res1=res1+res;
}
list2[f]=res1;
f++;
}

for(k=0;k<n-1;k++)
{
if(list2[k]<list2[k+1]) num=list2[k];
}
cout<<num;
return 0;
}
The input examples are:
3 4
7 1 7 9
4 10 8 8
9 2 6 3

The outpout is 10.

There are a number of logical problems with your code. Some of them are more likely to be noticed if you used more descriptive variable names. What is f?

What's happening with your code right now is that the condition in if (list2[k] < list2[k+1]) will never be true so num will never be assigned any value and you will output the junk that happened to be there when the variable was created.
Last edited on
I used f variable to be assigned to the list2[f] array,so it can increment from 0 to n-1 ,because list2[f] array represents the sum of each row after the calculations.
But I really do not understand why my condition will never be true. Thank you!
I used f variable to be assigned to the list2[f] array,so it can increment from 0 to n-1

Then perhaps you ought to trace the code, because f will never be anything other than 0 or 1.

But I really do not understand why my condition will never be true. Thank you!

Trace the code. Run through it with a debugger and inspect variable values.
Topic archived. No new replies allowed.