Passing a Value between an Array and Another Function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2006
Posts: 9
Reputation: Saint48198 is an unknown quantity at this point 
Solved Threads: 0
Saint48198 Saint48198 is offline Offline
Newbie Poster

Passing a Value between an Array and Another Function

 
0
  #1
Apr 12th, 2006
I'm trying to pass the valuee from the arrays for age, sex, and wTime to the cal_Fitness_Level function, but I can't seem to get it right. I either get the same level for everyone or garbage. code and data follows.

  1. #include <fstream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int cal_Fitness_Level(int age[], char sex[], int wTime[]);
  9. void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows);
  10.  
  11. int main ()
  12. {
  13. // step 1
  14. string lName[15];
  15. string fName[15];
  16. int age[15], wTime[15], i = 0, num = 0;
  17. int level[15];
  18. char sex[15];
  19. ifstream inFile; // input stream variable for data file
  20. ofstream outFile; // output stream variable for result data
  21.  
  22. inFile.open("data.txt");
  23.  
  24. if (!inFile) // step 3
  25. {
  26. cout << "Cannot open the input file." << endl;
  27. return 1;
  28. }
  29.  
  30. outFile.open("results.txt");// step 4
  31.  
  32. outFile << setfill(' ') << left << setw(15) << "Last Name" //header for output file
  33. << setfill(' ') << left << setw(22) << "First Name"
  34. << setfill(' ') << left << setw(7) << "Age"
  35. << setfill(' ') << left << setw(7) << "Gender"
  36. << setfill(' ') << left << setw(10) << "Walk Time"
  37. << setfill(' ') << left << setw(13) << "Fitness Level" << endl;
  38.  
  39. while (!inFile.eof())
  40. {
  41. inFile >> fName[i] >> lName[i] >> age[i] >> sex[i] >> wTime[i]; // step 5
  42. level[i] = cal_Fitness_Level(age, sex, wTime);// step 6
  43. i++;
  44. num++;
  45. }
  46.  
  47. sortData(lName, fName, age, sex, wTime, i);// step 7
  48.  
  49. for (i = 0; i < 9; i++)// step 8
  50. {
  51. outFile << setfill(' ') << left << setw(15) << lName[i]
  52. << setfill(' ') << left << setw(22) << fName[i]
  53. << setfill(' ') << left << setw(9) << age[i]
  54. << setfill(' ') << left << setw(7) << sex[i]
  55. << setfill(' ') << left << setw(11) << wTime[i]
  56. << setfill(' ') << left << setw(7) << level << endl;
  57. }
  58.  
  59. outFile << '\n' << "Number of records: " << num << endl;
  60.  
  61. return 0;
  62. }
  63.  
  64. int cal_Fitness_Level(int age[], char sex[], int wTime[])
  65. {
  66. int i = 0, fL;
  67.  
  68. if (age[i] >= 13 && age[i] <= 19)
  69. {
  70. if (wTime[i] >= 48)
  71. fL = 1;
  72. else if (wTime[i] > 43 && wTime[i] <= 47)
  73. fL = 2;
  74. else if (wTime[i] > 39 && wTime[i] <= 43)
  75. fL = 3;
  76. else if (wTime[i] > 35 && wTime[i] <= 39)
  77. fL = 4;
  78. else if (wTime[i] < 35)
  79. fL = 5;
  80. }
  81. else
  82. fL = 0;
  83.  
  84. return fL;
  85. }
  86.  
  87.  
  88. void sortData(string lName[], string fName[], int age[], char sex[], int wTime[], int noOfRows)
  89. {
  90. int i, j;
  91. int min;
  92.  
  93. // selection sort
  94. for (i = 0; i < noOfRows - 1; i++)
  95. {
  96. // step a
  97. min = i;
  98.  
  99. for (j = i + 1; j < noOfRows; j++)
  100. if (lName[j] < lName[min])
  101. min = j;
  102.  
  103. if(min!=i)// step b
  104. lName[i].swap(lName[min]);
  105. fName[i].swap(fName[min]);
  106. age[i] = age[min];
  107. sex[i] = sex[min];
  108. wTime[i] = wTime[min];
  109. }
  110. }

Michael Brooks 13 M 33
Amy Shields 30 F 40
Clara Miles 50 F 30
Robert Davidson 20 M 45
Joshua Chase 25 M 42
Jackie Choker 20 F 29
Sarla Kothari 60 F 37
George Runner 53 M 49
Sally Jones 19 F 47
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Passing a Value between an Array and Another Function

 
0
  #2
Apr 12th, 2006
Try
  1. level[i] = cal_Fitness_Level(age[i], sex[i], wTime[i]);
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 9
Reputation: Saint48198 is an unknown quantity at this point 
Solved Threads: 0
Saint48198 Saint48198 is offline Offline
Newbie Poster

Re: Passing a Value between an Array and Another Function

 
0
  #3
Apr 12th, 2006
Originally Posted by Salem
Try
  1. level[i] = cal_Fitness_Level(age[i], sex[i], wTime[i]);
I get a compiler error (C2662) when I try that. I do not think that you can reference an array in that format when calling a function.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Passing a Value between an Array and Another Function

 
0
  #4
Apr 12th, 2006
Of course you do.

Biut it makes no sense to pass the whole array to the function each time, when you're only reading into the array one element at a time.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 1
Reputation: CupOfCrazy is an unknown quantity at this point 
Solved Threads: 0
CupOfCrazy CupOfCrazy is offline Offline
Newbie Poster

Re: Passing a Value between an Array and Another Function

 
0
  #5
Apr 12th, 2006
Originally Posted by Saint48198
I get a compiler error (C2662) when I try that. I do not think that you can reference an array in that format when calling a function.
If you are only interested in a single value inside each of the arrays, passing a pointer to the array makes no sense, instead, change your function declaration to be:

  1. int cal_fitness_level(int age, int sex, int wTime);

and call it like this:

  1. level[i] = cal_fitness_level(age[i], sex[i], wTime[i]);
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 9
Reputation: Saint48198 is an unknown quantity at this point 
Solved Threads: 0
Saint48198 Saint48198 is offline Offline
Newbie Poster

Re: Passing a Value between an Array and Another Function

 
0
  #6
Apr 12th, 2006
That somewhat worked. The last three employees data is out of order. I've include the level variable in the sort function and modified the cal_fitness_level to look like Cup of Crazy's suggestion. I'm not sure why the last three are getting mixed up.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Passing a Value between an Array and Another Function

 
0
  #7
Apr 12th, 2006
> I'm not sure why the last three are getting mixed up.
Is the data OK before you sort it?

> if(min!=i)
Consider using a lot more { } in this function, because it seems to me that not everything is being run at the correct time. Yes, braces can be optional in some circumstances, but if you take the approach of always using them, you minimise your surprises later on.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 9
Reputation: Saint48198 is an unknown quantity at this point 
Solved Threads: 0
Saint48198 Saint48198 is offline Offline
Newbie Poster

Re: Passing a Value between an Array and Another Function

 
0
  #8
Apr 12th, 2006
I added the {} for if(min!=i) but there was no change
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Passing a Value between an Array and Another Function

 
0
  #9
Apr 13th, 2006
> Is the data OK before you sort it?
You didn't answer this question.

Post your latest code.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 9
Reputation: Saint48198 is an unknown quantity at this point 
Solved Threads: 0
Saint48198 Saint48198 is offline Offline
Newbie Poster

Re: Passing a Value between an Array and Another Function

 
0
  #10
Apr 13th, 2006
The data is fine before the sort takes place.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC