I don't even know where to start with this one.

Hello everybody...

I'm soldiering through a C++ class and have been doing alright with the problems so far but this one kinda stumps me as my classes before this didn't use infiling that much.

The gist of the problem is to make a total for all candies in a factory using arrays

There are 25 types of candies and the number of batches

So I'd think the issue is Make three - four arrays

Read in from the Txt file - It's ordered to where it's Candy type and then the size of the batch, example 20 7000
One to hold the Candy types
one to hold the Batch
Another array to hold the batch totals for each type

Outfile that


I however, am having difficulties going about that namely get the data to infile correctly which I think if i can figure that part out getting the rest of it to total up should be much easier.

Any tips would be helpful to me.

closed account (o3hC5Di1)
Hi there,

Reading in the file into the arrays could be like this:

1
2
3
4
5
6
7
8
9
10
int candies[25];
int batches [25];
int i = 0;
ifstream ifs("input.txt");

while (ifs.good() && i <=25) //while we can read from the file and won't go out of array size
{
    ifs >> candies[i] >> batches[i];  //read line, first number is candy, second is batch
    i++; //increment the counter
}


Hope that clarifies it a bit for you.
Do come back to us with some of your own code if you need any further help.

All the best,
NwN
Hmmm

then it only be a matter of making function loops to get the totals of each candy type wouldn't

since their read in the same the two should correlate right?

IE Candies[3] has the Value of 5, The candy type and it's companion Batch [3] would have the number of that batch for that processing order of 200

There if I made a loop that look through the Array looking for 5's in candies and then adding the value in the Batch array that corresponds to the location. I would in theory be able to find the totals for each type


Thank you I beleive you gave me the first step i need in figuring this one out.
closed account (o3hC5Di1)
Hi there,

If you want to keep the correlation clearer in your code, you could make a 2 dimensional array:

int candies[25][2];

This will hold 25 mini-arrays of two elements each, taking your example, candy[3] would then be:

1
2
candies[3][0] //this would be value 5, the candy number
candies[3][1] //this would be the batch number, 200 


In my previous code you would replace:

ifs >> candies[i] >> batches[i]; //read line, first number is candy, second is batch

With:

ifs >> candies[i][0] >> candies[i][1];

You could make the code clearer using an enum:

1
2
enum { CANDY=0, BATCH=1 };
ifs >> candies[i][CANDY] >> candies[i][BATCH];


Hope that helps.

All the best,
NwN
No he just wants it done in 1D arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 ifstream infile;

 int candies[250];
int batches [250];
int i = 0;

infile.open( "chocolate.txt");


while (infile.good() && i <= 250)
{
    infile >> candies[i] >> batches[i];
    i++;
}


infile.close(); 



It keeps basically crashing or given odd numbers garbage numbers back
Sample of how the numbers are displayed in the input file

12 1026
15 248
7 402
10 1076
16 948
14 1174
6 1137
8 719
9 952
14 1516
9 830
14 296
17 596
15 874
23 100
15 1063
16 867

Should i tell it to endl?

I got it load!... but the [0] postion is giving a wierd number... every other postion works but that one.
Last edited on
The input loop has issues in general, it will make problems when the file ends in something other than a number, and will crash when i == 250. For the second problem, just make the loop terminate if i < 250.

The first issue is solved by moving the actual extraction into the while condition:
1
2
3
4
while (input >> number01[i] >> number02[i] && i < SIZE)
{
  i++;
}


Dealing with the lone i++ might get you bonus points with your teacher:
1
2
while (input >> candies[i] >> batches[i] && i++ < SIZE)
  ;


The output always the same:
1
2
for (unsigned int j = 0 ; j < i ; j++)
  std::cout << candies[j] << " " << batches[j] << '\n';


BTW, SIZE is const int:
1
2
3
const unsigned int SIZE = 250;
int candies[SIZE];
int batches[SIZE];
Last edited on
Thanks, your advice helped and it fixed the [0] issue i was having


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int totalb1 (int c[], int b[])
{
    int btotal1 =0 ;
    int i = 0;
    while (!(i<=250))
   {
      if (c[i]==1)
      {
          btotal1 = btotal1 + b[i];
               i =i+1;
      }

      else
      {
           i =i+1;
      }

   }

    return btotal1;
}


This is the function i am gonna use to make the different batch totals -one loop function for each batch, so get one working it's just a matter of copy/pasting/editing the function to meet the other types-

It's suppose to in theory, feed the Arrays in, then using the loop find the Candy spots with the matching type Here its type 1

then take the candy total form the correpsonding Batch [] and add it to a total and move on...

I keep getting Zero back. Am I not passing the Arrays in correctly?
Last edited on
You still haven't quite grasped a couple of important concepts.

When you define an array like this:
int someNumbers[10];
your array has space enough to hold ten integers.
The first integer is accessed with the index 0. The tenth integer in this array is accessed with the index 9. In other words, the integer in the tenth position is offset nine positions from the position of the first integer.

So, you want your while loop to stop when the index is greater than the index of the last integer in the array.


1
2
3
4
5
6
int index = 0;
while (index < 10) {

    // ... 
    index ++;
}   


When this runs, index will be assigned in turn the values : 0, 1, 2 ,3 ,4, 5, 6, 7, 8, 9, 10. When it reaches ten, the expression index < 10 will be false, so the loop will no longer run.
If you use the expression index <=10, when index holds the value ten, the condition is still true, and the loop will continue to run one more time, in which case
someNumbers[index]
will be equivalent to
someNumbers[10]
which will attempt to access the eleventh element of the array. In other words, you will read or write past the end of the array.

The expression you have used here
!(i<=250)
will be false when i is 0, so your loop will never run.

Second, the greatest value of a function is that it can be generalized. You do not need to write another function for each number that you would like to search for. By adding that value as a parameter, you can reuse the function you have already written.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int totalb (int c[], int b[], int candyType)
{
    int btotal =0 ;
    int i = 0;
    while (i < 250)  {

      if (c[i] == candyType)  {

          btotal = btotal + b[i];
      }
      i++;
   }

    return btotal;
}


Now you can use that function like this:
1
2
3
4
5
6
7
8
9

const unsigned int NUM_CANDY_TYPES = 8; // for example
int typeTotal[NUM_CANDY_TYPES];

for (int typeCode = 0; typeCode < NUM_CANDY_TYPES, typeCode++ ) {

    typeTotal = totalb(c, b, typeCode);
}


This assumes, of course, that you know the greatest number used as a candy type.
Last edited on
I had it backwards... now don't i feel stupid for something so simple

I appreciate the advice
This should be my last issue since eveyrthing else works


I modified the total function so I can figure averages of batches

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int baverager (int c[], int b[], int type)
{
    int btotal =0 ;
    int i = 0;
    int n = i;
    while (!(i>250))
   {
      if (c[i]==type)
      {
          btotal = btotal + b[i];
               i =i+1;
               n = n++;

      }

      else
      {
           i =i+1;
      }

   }

   int baverage = btotal / n;

    return baverage;



however it would crash when it comes to averaging the three types that have zero in them,

I figured a simple if else check at the end would solve the issue either N or Btotal = 0 would mean no matches were found there fore zero, and it wouldn't try to divide


Yet two wierd things happend, the check either slightly altered the averages of the working batches from slight average changer 713 to 817 or to averages bigger then the full total of them all combined

and it would still crash on those types that had zero?

I'm pretty purplexed at why the check isn't working.


Figured it out, my program works Yay!
Last edited on
Please post your solution.
okay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
int baverager (int c[], int b[], int type)
{
    int btotal =0 ;
    int i = 0;
    int n = 0;
    while (!(i>250))
   {
      if (c[i]==type)
      {
          btotal = btotal + b[i];
               i =i+1;
               n = n++;

      }

      else
      {
           i =i+1;
      }

   }

   int baverage;
// checks for Zero batches to prevent a divide by zero incident
   if (n > 0)
   {
       baverage = btotal/n;
   }
   else
   {
       baverage = 0;
   }
//Candy type is read in concurrent with the is batch size thus the number is found in the matching batch array
    return baverage;

You need to look harder at this expression:
!(i>250)
When i = 250, will that be true or false?
it be false
Last edited on
Is 250 greater than 250?
No it's equal, but the expression i picked is when i is Greater then 250 so it will input to 250
!(i250>250) = !(FALSE) = TRUE

And your loop will iterate over all the values from 0 to 250.

Place your left hand palm down in front of you.
Beginning at your little finger and moving to the right, number each finger starting with 0. When you get to your thumb, what number are you on?

If you have an array of five elements and the first element is at index 0, at what index is the last element?
Last edited on
Topic archived. No new replies allowed.