HI, I have a prog. assignment, I did most of it, but I couldn't write some functions properly, so try 2 help.?

these are the functions I wrote b4.

#include "Array.h"
#include <iostream> 
using namespace std;

Array::Array() //constructor to set size=6
{
size=6;
}

void Array::read() // read array elements
{
cout<<"Enter "<<size<<" Array Elements:\n";
for (int i=0; i<size; i++)
cin>>A[i];
}

void Array::display() // display array elements
{
for (int i=0; i<size; i++)
cout<<A[i]<<"\t";
cout<<endl;
}

int Array::max () //find array maximum
{
int max = A[0];
for (int i=1; i<size; i++)
if (A[i]>max)
max=A[i];
return max;
}

int Array::min () //find array minimum
{
int min = A[0];
for (int i=1; i<size; i++)
if (A[i]<min)
min=A[i];
return min;
}

bool Array::search (int key) //search array for specific key
{
for (int i=0; i<size; i++)
if (A[i]==key)
return true;
return false;
} 

void Array::reverse () //reverse array elements 
{
int mid=size/2;
int temp;
for (int i=0; i<mid; i++){
temp=A[i];
A[i]=A[size-1-i];
A[size-1-i]=temp;
}
}

void Array::sort () //sort array elements with ascending order 
{
int temp;
for (int i=size-1; i>0; i--)
for (int j=0; j<i; j++)
if (A[j]>A[j+1]){
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
}

I need to add these functions also (I tried but there were many errors :S )

void replace (int x, int y): a function that is used to replace an existing array value x with another value y.
int count (int ele): a function that is used to count the number of times the value ele appears in the array.
void split (): a function that is used to split the array into two equal halves and store them in two different arrays.

THANKS

Salem commented: Neither urgent, nor important. And the lack of code tags labels you an ignorant waste of space - READ THE FSKING INTRO THREADS -3
Ancient Dragon commented: Ditto for what Salem said. -5

Recommended Answers

All 5 Replies

I need to add these functions also (I tried but there were many errors :S )

void replace (int x, int y): a function that is used to replace an existing array value x with another value y.
int count (int ele): a function that is used to count the number of times the value ele appears in the array.
void split (): a function that is used to split the array into two equal halves and store them in two different arrays.

THANKS

Please say what you have already tried?

A hint for #2..
You'll need a variable that increments each time ele appears.

that what I tried:

void Array::replace()
  {
	  for(int i=0;i<6;i++){
		  if(A[i]==x)
			  A[i]=y;
	  }
   }
  int Array::count(int ele){
	  int c=0;
	  for(A[i]==ele){
		  c+=1;
		  return c;
	  }
  }
  void Array::split(){
	  for(int i=0;i<3;i++)
		  B[i]=A[i];
	  for(int i=3;i<6;i++){
		  for(	int j=0;j<3;j++){
			  c[j]=A[j];
			  c[0]=A[3];
			  c[1]=A[4];
			  c[2]=A[5];
		  }
	  }
  }

You're missing the parameters in the signature. It should be

void Array::replace(int x, int y)

that what I tried:

 void Array::replace()
  {
      for(int i=0;i<6;i++){
          if(A[i]==x)
              A[i]=y;
      }
   }

end quote.

Close. Clue: Use an if statement.
Note: You can also use c++; instead of c+= 1; in order to increment.

start quote:

  int Array::count(int ele){
      int c=0;
      for(A[i]==ele){
          c+=1;
          return c;
      }
  }

end quote.

PLz I need more help, becoz I did it all, but still there r many errors :S

please post the errors -> (or/and) code where error occurs.

without, we can't help you.

Daniel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.