Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

Arrays/Parallel Arrays with a infile.

i created this code, so it can take the values from a infile into an array. i have 2 functions, one that will take the values from the infile and filled up the array while keeping a running count of the number of values stored in the array. with a length variable. Another function that sets the elements of passing to True whenever the parallel value of scores is 25 or above.

everything seems to be working up to the point when its time to use the boolean array in the second function.  the first part seems to work fine, its showing a count of 8 values stored into the scores array.

but i dont see the second part not activating at all.
#include <iostream>
#include <fstream>
using namespace std;

//function prototypes

void getData(ifstream&,int [], int&);
int processPass(bool [],int [], int&);



int main()
{
	int scores[100];
	bool passing[100];
	int length= 0;
	
	ifstream infile;

	infile.open ("c:\\practicedata.txt");

	if (!infile)
		cout<<"File not found";
  
	getData(infile, scores, length);


  cout<<length;
    processPass(passing,scores,length);

	
	

	 infile.close();
return 0;
}

 void getData(ifstream &infile, int scores[], int &length)
 {
	 

		infile>>scores[length];
	do
	{
		length++;

		infile>>scores[length];
	}
	while (infile);

	 }

 int processPass(bool passing[],int scores[], int &length)
 {
	

	 if ( scores[length] >= 35)
	 passing[length]=true;
    return passing[length];
 }

Open in new window

practicedata.txt
0
Ajs135
Asked:
Ajs135
  • 3
  • 3
1 Solution
 
phoffricCommented:
You have a loop in your getData function - good.
You do not have a loop in your processPass function, nor are you looping over processPass in the main function. If you want to grade every entry, then add a loop to go over every grade.
0
 
phoffricCommented:
Some other concerns..

1. processPass is returning an int, but your return statement is return passing[length]; and so you are returning a bool - recommend consistency. I notice at the moment you are not using the return value of processPass, but I'm assuming that you plan on doing that later. Actually, since processPass purpose seems to be to set the passing array, then I'm not sure why processPass needs to return anything at this time. (It could just be set to return void, and then you would not use a return statement.)

2. processPass is setting a passing element in the array to true if score > 35. But what if score < 35? I think you should be explicit in what it should be set to true or false for the first "length" elements in your array.

3. In your main function, I suggest that you initialize the entire passing array to false before sending it to the processPass function. Even though if you follow suggestion #2, this appears to be unnecessary, in general it is a good idea to initialize variables or arrays especially if they are being set in another function.

4. This problem will likely go away after you add the loop - in processPass, you have:
    if ( scores[length] >= 35)
         passing[length]=true;
Just be aware that the elements in scores that are being set have indexes 0..length-1; so scores[length] does not identify an element that was set by the getData function.

Hope this helps.
0
 
js-profiCommented:
adding to phoffric's comment i would like suggest to using a struct for combining score and passing. that way you have only 1 array and the struct can init the elements. currently your array elements were not initialized.
#include <iostream>
#include <fstream>
using namespace std;

// data structure
struct Data
{
   int score;
   bool passing;
   // would init any instance
   Data() : score(0), passing(false) {}
 
};

//function prototypes

int getData(ifstream&, Data[], int);
int processPass(Data[], int);

int main()
{
   Data data[100];   
   int length= 0;
	
   ifstream infile("c:\\practicedata.txt");
   if (!infile)
     cout<<"File not found";
  
   length = getData(infile, data, 100);
   cout<<length;
   int count35 = processPass(data, length);
   infile.close();
   return 0;
}

int getData(ifstream &infile, Data data[], int size)
{
   int length = 0;
   while (length < size && 
          infile>>data[length++].score);
   return length;
}
 int processPass(Data data[], int length)
 {
    int count = 0;
    for (int i = 0; i < length; ++i)
    {
       if (data[i].passing = (data[i].score >= 35))
           count++;
    }
    return count;
 }

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ajs135Author Commented:
phoffric:

1) So would it be better to turn the function into a void function instead, like you say im not returning any values.?

2)if ( scores[length] >= 35)
         passing[length]=true;
      else  {
    passing[length]=false;                         would that be better ?


0
 
js-profiCommented:
turning it into a void is only a less important correction. more important is to add a loop that would run from 0 to length-1 and updates all passing elements. also you need to initialize passing array with all items false.
0
 
phoffricCommented:
re: "So would it be better to turn the function into a void function instead, like you say im not returning any values.?"
Yes. I recommend that the function return void in order for its declaration to better communicate its intent. This is known as self-documenting code. For short assignments in school or self-learning, you can do as you have done. But, when you are trying to write programs for industry, then there are other factors to consider. You may stop working on your program for a year and come back to it later to modify it. Then you may wonder why you are returning an int - did you forget to do something? It may only take you a few minutes to realize that all is OK; but multiply this few minutes lost by the 1000's of functions you will write; and it all adds up. When you work in teams whose members change of years, someone looking at your code will be temporarily confused, and may try to contact you (if you are still on that project). Now more time is wasted. All for want of a void.

re: if ( scores[length] >= 35)
         passing[length]=true;
      else  { // score < 35
    passing[length]=false;     would that be better ?

Yes! In fact, on my last project, there was a rule that every "if" must have an "else" (even if there was nothing in the else! Maybe that is an extreme, but it eliminates the bugs where someone forgets to include the else, and there is a scenario that wasn't originally considered where the else portion would have been essential.
0
 
js-profiCommented:
alternatively to a general else you may first assign the default (else) and then add the if and else if cases:

    passing[length]=false;
    if ( scores[length] >= 35)
         passing[length]=true;

that isn't recommendable for complex class types, though.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now