Sign in with
Sign up | Sign in
Your question

C++ array program

Tags:
  • Programming
  • Apps
Last response: in Apps General Discussion
Share
September 17, 2011 9:43:52 AM

Hello all, new semester has begun and got a few questions on my first project. Ill start off with the errors then get into some of the methods that I need help with.

  1. #include <string>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class MyArray
  7. {
  8. private:
  9. int array[10];
  10.  
  11. public:
  12. MyArray();
  13. MyArray(int value);
  14. void Set(int index, int value);
  15. int Get(int index);
  16. int Size();
  17. int FindFirst(int value);
  18. int FindLast(int value);
  19. void Initialize();
  20. void Initialize(int value);
  21.  
  22.  
  23. }



  1. #include "stdafx.h"
  2. #include "MyArray.h"
  3.  
  4. MyArray::MyArray(){
  5. array[0] = 0;
  6. array[1] = 0;
  7. array[2] = 0;
  8. array[3] = 0;
  9. array[4] = 0;
  10. array[5] = 0;
  11. array[6] = 0;
  12. array[7] = 0;
  13. array[8] = 0;
  14. array[9] = 0;
  15. }
  16.  
  17. MyArray::MyArray(int value){
  18. array[0] = value;
  19. array[1] = value;
  20. array[2] = value;
  21. array[3] = value;
  22. array[4] = value;
  23. array[5] = value;
  24. array[6] = value;
  25. array[7] = value;
  26. array[8] = value;
  27. array[9] = value;
  28. }
  29.  
  30.  
  31. void MyArray::Set(int index, int value)
  32. {
  33. if (index < 0 || index > 9)
  34. cout << "Not a valid index";
  35. else
  36. array[index] = value;
  37. }
  38.  
  39. int MyArray::Get(int index)
  40. {
  41. return array[index];
  42. }
  43.  
  44. int MyArray::Size()
  45. {
  46. int arrSize = sizeof(array)/sizeof(int);
  47. return arrSize;
  48. }
  49.  
  50. int MyArray::FindFirst(int value)
  51. {
  52. for (int i = 0; i<10; i++)
  53. {
  54. if (array[i] == value)
  55. return i;
  56. }
  57.  
  58. }
  59.  
  60. int MyArray::FindLast(int value)
  61. {
  62.  
  63. }
  64.  
  65. void MyArray::Initialize()
  66. {
  67. array[0] = 0;
  68. array[1] = 0;
  69. array[2] = 0;
  70. array[3] = 0;
  71. array[4] = 0;
  72. array[5] = 0;
  73. array[6] = 0;
  74. array[7] = 0;
  75. array[8] = 0;
  76. array[9] = 0;
  77. }
  78.  
  79. void MyArray::Initialize(int value)
  80. {
  81. array[0] = value;
  82. array[1] = value;
  83. array[2] = value;
  84. array[3] = value;
  85. array[4] = value;
  86. array[5] = value;
  87. array[6] = value;
  88. array[7] = value;
  89. array[8] = value;
  90. array[9] = value;
  91. }


  1. // MyArray.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MyArray.h"
  6.  
  7.  
  8. int _tmain(int argc, _TCHAR* argv[])
  9. {
  10.  
  11. MyArray ar;
  12.  
  13. ar.Set(0,50);
  14. ar.Set(4,100);
  15. ar.Get(0);
  16. ar.Get(4);
  17. ar.Size();
  18.  
  19. return 0;
  20. }




ERROR:

Error 1 error C2628: 'MyArray' followed by 'int' is illegal (did you forget a ';'?)

Error 2 error C3874: return type of 'wmain' should be 'int' instead of 'MyArray'

Error 3 error C2533: 'MyArray::{ctor}' : constructors not allowed a return type



For method FindLast(int value) I need to return the index of the last occurrence of the given value in the array. I got FindFirst(int value) but how would I find the last? Like if index 1 and 5 have value of 50 in it, how do I print out 5 and not 1?

More about : array program

September 17, 2011 9:47:53 AM

Found the errors, forgot the semicolon at the end of the class, was doing java for a while and don't think you need the semicolon in java classes. =/ So just the method help for now. =D
m
0
l
September 17, 2011 10:21:28 AM

Hmmm for get the last occurrence would I just search through the array starting at the last element and working my way to the first?
m
0
l
!