C++ class file that uses an array

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

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

C++ class file that uses an array

 
0
  #1
Apr 28th, 2006
Hi,
Can anyone help with a project I'm working on I'm new to classes
and I got really confused while trying to convert a simple console application in to a class file. I would appreciate some help if anyone willing to.
Following is the code I am trying to convert
  1. #include <iostream.h>
  2. const int MAX = 15;
  3. void get_values(int []);
  4. void print_values (int []);
  5. void sort_values (int [], int);
  6. void search_values (int []);
  7. int main()
  8. {
  9. int values[MAX];
  10. get_values(values);
  11. sort_values(values, MAX);
  12. print_values(values); //This will allow user to see the sorted order of the values entered if enabled.
  13. search_values(values);
  14. return 0;
  15. }
  16. void get_values(int value[])
  17. {
  18.  
  19. for (int i=0; i < MAX; i++)
  20. {
  21. cout << "Please enter value #" << i+1 <<" : ";
  22. cin >> value[i];
  23. }
  24. }
  25.  
  26. void print_values (int value[])
  27. {
  28. cout<<endl;
  29. for (int i=0; i<MAX; i++)
  30. {
  31. cout << value [i] << endl;
  32. }
  33. cout<<endl;
  34. }
  35. void sort_values (int value[], int maxval)
  36. {
  37. int i, j, temp, flag;
  38. for(i=maxval-1; i>0; i--)
  39. {
  40. flag = 1;
  41. for(j=0; i>j; j++)
  42. {
  43. if(value[j]>value[j+1])
  44. {
  45. flag = 0;
  46. temp = value[j];
  47. value[j] = value[j+1];
  48. value[j+1] = temp;
  49. }
  50. }
  51. if(flag)
  52. break;
  53. }
  54. }
  55. void search_values (int value[])
  56. {
  57. int check;
  58. cout << endl <<"Please enter the value you want to search for: ";
  59. cin >> check;
  60. for (int i=0; i<MAX; i++)
  61. {
  62. if (value[i] == check)
  63. {
  64. cout << "The value you searched for found in position #" << i+1 << endl;
  65. }
  66. }
  67. }

I come up with the following code while working on this project but I couldn't figure out what I did wrong.

#include <iostream.h>
#include "p6.h"
project::project()
{
value[15];
}
int project::get_value()
{
return value[15];
}
void project::sort_values(int value[])
{
int i, j, temp, flag;
int maxval = 15;
for(i=maxval-1; i>0; i--)
{
flag = 1;
for(j=0; i>j; j++)
{
if(value[j]>value[j+1])
{
flag = 0;
temp = value[j];
value[j] = value[j+1];
value[j+1] = temp;
}
}
if(flag)
break;
}
}
void project::search_values(int value[])
{
int check;
cout << "Please enter the value you want to search for: ";
cin >> check;
for (int i=0; i<15; i++)
{
if (value[i] == check)
{
cout << "The value you searched for found in position #" << i+1 << endl;
}
}
}
******************************************
#ifndef P6_h
#define P6_h
class project
{
public:
project();
int get_value();
void sort_values(int value[]);
void search_values(int value[]);
private:
int value[15]; 
};
#endif
******************************************
 
#include <iostream.h>
#include "p6.h"
void get_values (int []);
void print_values (int []);
 
int main()
{ 
project myProject;
int count = 15;
int value[15]; 
get_values(value[]);
return 0;
}
void get_values(int values[])
{
for (int i=0; i<15; i++)
{
project.get_value (values[i]);
cout <<"Enter Values: ";
cin >> values[i];
}
}


I really appreciate if someone can point me a way out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 26
Reputation: dude543 is an unknown quantity at this point 
Solved Threads: 0
dude543 dude543 is offline Offline
Light Poster

Re: C++ class file that uses an array

 
0
  #2
Apr 28th, 2006
The declatrion of class is good.
  1. class project
  2. {
  3. public:
  4. project();
  5. int get_value();
  6. void sort_values(int value[]);
  7. void search_values(int value[]);
  8. private:
  9. int value[15];
  10. };

So, why did declare "int value[15]" again in function main.

And what is this :
  1. project::project()
  2. {
  3. value[15];
  4. }
what does that do ?
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC