Question
I have to create two arrays. One will store ID numbers from 1-10 and the other will record the number of boxes sold. The first time an identification number is read, it is stored in the next free slot of the array for ID numbers and the corresponding position in the array of boxes sold is initialized. If the ID number is inputted again by the user, the number of boxes sold is added to the number of boxes sold to the corresponding position in the array of boxes sold. I have t assume that each group sold at least one box. I do not want to post the actual problem because I want to work it out for myself, but I would really appreciate some guidance. My impression is that the ID number 1-10 can serve as the id array for example: int [] idNumbers = new int [10] but if this is the case, when I ask the user for the id and boxes sold on each receipt, I don't know how the array will populate. I would really appreciate an example. Is this like a counter array? Do I have to make classes for the ID number and the boxes sold even though I do not know the boxes sold?
Explanation / Answer
You are right. The number of boxes sold would work exactly like an array of counters. when you input id number, you would iterate through idNumbers and make sure it is not already inputed. If it is, you would increment at that index in boxes array. some code snippets .... //parallel arrays int [] idNumbers = new int [10]; int[] BoxesSold=new int[10]; int index; int id, boxes; int stores=0; id=nextInt(); boxes=nextInt(); index=foundAt(idNumbers,id,stores);//assumes theres a fxn that returns index where id is found in idNumbers or -1 if not found if(index==-1){ idNumbers[stores]=id; BoxesSold[stores]=boxes; stores++; } else{ BoxesSold[index]+=boxes; } ... static int foundAt(int idNumbers[],int target,int count){ if(count==0) return=1; for(int i=0;i