C++ array program

Status
Not open for further replies.

laserpp

Distinguished
Nov 29, 2008
356
0
18,780
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.

Code:
#include <string>
#include <iostream>

using namespace std;

class MyArray
{
private:
	int array[10];

public:
	MyArray();
	MyArray(int value);
	void Set(int index, int value);
	int Get(int index);
	int Size();
	int FindFirst(int value);
	int FindLast(int value);
	void Initialize();
	void Initialize(int value);


}


Code:
#include "stdafx.h"
#include "MyArray.h"

MyArray::MyArray(){
	array[0] = 0;
	array[1] = 0;
	array[2] = 0;
	array[3] = 0;
	array[4] = 0;
	array[5] = 0;
	array[6] = 0;
	array[7] = 0;
	array[8] = 0;
	array[9] = 0;
}

MyArray::MyArray(int value){
	array[0] = value;
	array[1] = value;
	array[2] = value;
	array[3] = value;
	array[4] = value;
	array[5] = value;
	array[6] = value;
	array[7] = value;
	array[8] = value;
	array[9] = value;
}


void MyArray::Set(int index, int value)
{
	if (index < 0 || index > 9)
		cout << "Not a valid index";
	else
		array[index] = value;
}

int MyArray::Get(int index)
{
	return array[index];
}

int MyArray::Size()
{
	int arrSize = sizeof(array)/sizeof(int);
	return arrSize;
}

int MyArray::FindFirst(int value)
{
	for (int i = 0; i<10; i++)
	{
		if (array[i] == value)
			return i;
	}

}

int MyArray::FindLast(int value)
{

}

void MyArray::Initialize()
{
	array[0] = 0;
	array[1] = 0;
	array[2] = 0;
	array[3] = 0;
	array[4] = 0;
	array[5] = 0;
	array[6] = 0;
	array[7] = 0;
	array[8] = 0;
	array[9] = 0;
}

void MyArray::Initialize(int value)
{
	array[0] = value;
	array[1] = value;
	array[2] = value;
	array[3] = value;
	array[4] = value;
	array[5] = value;
	array[6] = value;
	array[7] = value;
	array[8] = value;
	array[9] = value;
}

Code:
// MyArray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "MyArray.h"


int _tmain(int argc, _TCHAR* argv[])
{

	MyArray ar;

	ar.Set(0,50);
	ar.Set(4,100);
	ar.Get(0);
	ar.Get(4);
	ar.Size();

	return 0;
}



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?

 

laserpp

Distinguished
Nov 29, 2008
356
0
18,780
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
 
Status
Not open for further replies.

TRENDING THREADS