?
Solved

Arrays/Parallel Arrays  with a infile.

Posted on 2010-01-11
7
Medium Priority
?
444 Views
Last Modified: 2013-12-14
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
Comment
Question by:Ajs135
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 26290398
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
 
LVL 32

Expert Comment

by:phoffric
ID: 26290460
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
 
LVL 7

Expert Comment

by:js-profi
ID: 26292293
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
Independent Software Vendors: 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!

 

Author Comment

by:Ajs135
ID: 26294826
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
 
LVL 7

Expert Comment

by:js-profi
ID: 26295208
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
 
LVL 32

Accepted Solution

by:
phoffric earned 2000 total points
ID: 26295885
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
 
LVL 7

Expert Comment

by:js-profi
ID: 26321532
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question