Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <ostream>
using namespace std;

class Array
{
public:
    int A[10];
    int size;
    int length;

public:
    void Display();
    int Get(int index);
    int Set(int index,int x);
    int Max();
    int Min();
    int Sum();
    float Average();
};

void Array::Display()
{
    int i;
    cout << "Elemnts are : ";
    for (i = 0; i < length; i++)
    {
        cout << A[i] << endl;
    }
}

int Array::Get(int index)
{
  
   if(index>=0 && index<length)
   return A[index];
  
}

int Array::Set(int index,int x)
{
    {
        if(index>=0 && index<length)
          A[index]=x;
    }
    return -1;
}

int Array::Max()
{
    int max=A[0];
    int i;

    for(i=1; i<length; i++)
    {
        if(A[i]>max)
        max=A[i];
    }
   return max;
}

int Array::Min()
{
  int min=A[0];

  for(int i=1; i<length; i++)
  {
      if(A[i]<min)
      min=A[i];
  }
  return min; 
}

int Array::Sum()
{ 
    int total=0;
    for(int i=0;i<length;i++)
    {
        total=total+A[i];
    }
    return total;
}

float Array::Average()
{
    int total=0;
    for(int i=0; i<length;i++)
    {
        total=total+A[i];
    }
    return total/length;
}

int main()
{
    Array arr={8,3,9,15,6,10,7,2,12,4};
    
    cout<<arr.Get(4)<<endl;//im getting garbage value here
}


What I have tried:

im new to this and im not able to recognise the mistake in this code
Posted
Updated 2-Nov-21 21:22pm

Your initialization
C++
Array arr={8,3,9,15,6,10,7,2,12,4};
is ineffective.

Try
C++
#include <ostream>
#include <initializer_list>
using namespace std;

class Array
{
  enum { CAPACITY = 10 };
public:
    int A[CAPACITY];
    int size;
    int length;

public:
    Array(initializer_list<int> l);
    void Display();
    int Get(int index);
    int Set(int index,int x);
    int Max();
    int Min();
    int Sum();
    float Average();
};

Array::Array(initializer_list<int> l)
{
  length = 0;
  for (auto i : l)
  {
    if ( length >= CAPACITY) break;
    A[length] = i;
    ++length;
  }
}

//...

int Array::Get(int index)
{

   if(index>=0 && index<length)
    return A[index];
  return -1;
}

//...

int main()
{
    Array arr = {8,3,9,15,6,10,7,2,12,4};

    cout<<arr.Get(4)<<endl;
}
 
Share this answer
 
v2
Comments
Sanjana Alam 3-Nov-21 7:43am    
thanks,it worked!
CPallini 3-Nov-21 7:59am    
You are welcome.
That code won't even compile: your Get function has a code path where no result is returned - when the if condition fails. You will need to decide what to do at that point, and how to return or handle an error at runtime.
Then to fix the problem you have noticed, replace this:
C++
Array arr={8,3,9,15,6,10,7,2,12,4};

With this:
C++
Array arr;
int i = 0;
arr.Set(i++, 8);
arr.Set(i++, 3);
arr.Set(i++, 9);
arr.Set(i++, 15);
arr.Set(i++, 6);
arr.Set(i++, 10);
arr.Set(i++, 7);
arr.Set(i++, 2);
arr.Set(i++, 12);
arr.Set(i++, 4);
 
Share this answer
 
Comments
Sanjana Alam 3-Nov-21 7:47am    
thank you!
OriginalGriff 3-Nov-21 7:59am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900