This is a program that takes an array and print the sum of the odd (named m_sumOdd) and even (named m_sumEven) numbers of that array using class. However when i run it and enter some values like (4, 6, 9, 3, 1) the m_sumEven returns 10 and m_sumOdd returns 2037769787. What's the problem with m_sumOdd ?
#include <iostream>
#include <string>
class myClass {
private:
int m_sumEven;
int m_sumOdd;
public:
myClass() {
m_sumEven = 0;
m_sumOdd = 0;
}
myClass(int arr[]) {
for (int i = 0; i < 5; i++)
{
if (arr[i] % 2 == 0) {
m_sumEven += arr[i];
}
else if (arr[i] % 2 != 0) {
m_sumOdd += arr[i];
}
}
print();
}
void print() {
std::cout << m_sumEven << "\t" << m_sumOdd << std::endl;
}
};
int main(){
int main_arr[5];
for (int j = 0; j < 5; j++)
{
std::cin >> main_arr[j];
}
myClass obj(main_arr);
std::cin.get();
}
From your comment:
the problem is from my uni textbook and it specify that the private variables shall be initialized in the default constructor and the other constructor find the sum
I'm not sure what solution the textbook is looking for, but there is a mechanism that allows one constructor to use another.
class myClass {
private:
int m_sumEven;
int m_sumOdd;
public:
myClass() {
m_sumEven = 0;
m_sumOdd = 0;
}
myClass(int arr[]): myClass() { // <-------
....
This is called delegating constructors. This makes your myClass(int arr[]) constructor use the myClass() constructor before continuing on.
When you do not initialize your variable in C++, the default value of that variable is whatever garbage value happens to already be in that memory location. So if you initialize your privates variables as zeros (as in your non-parameterized constructor), your problem will be solved.
The second constructor doesn't initialize the private variables like the first one.
m_sumEven = 0;
m_sumOdd = 0;
Put these to the second constructor as well.
Even better, put them at the declaration:
int m_sumEven = 0;
int m_sumOdd = 0;
And now you don't need the first constructor.
Unrelated: consider using std::initializer_list instead. This provides you a safe way to know how many items are passed to the constructor, while now you have a fixed 5 in mind which may change later.
Related
I need help with this code.
What I want is to make a parametric constructor and initialise/set the value of array in it.
Question: Make a class with arrays of integers and initialise it in a constructor. Then find the smallest and largest numbers using functions.
But I am stuck at how to initialise the array in the constructor.
I want to take data input in both ways
(1) By user, using cin
(2) By giving my own values
class Numbers
{
int Arr[3];
public:
Numbers() //default constructor
{
for (int i=0 ; i<=2 ; i++)
{
Arr[i]=0;
}
}
Numbers(int arr[]) //parameteric constructor
{
for (int i=0;i<=2;i++)
{
Arr[i]=arr[i];
}
}
};
int main()
{
int aro[3] = {0,10,5};
Numbers obj (aro);
return ;
}
The solution is pretty simple. I've made a new program from start again (for sake of understanding). According to your requirement, you wants to get input of array elements from the user dynamically and assign them to a constructor and use a method to print the highest value.
Consider the following code:
#include <iostream>
using namespace std;
const int N = 100;
class Numbers
{
int largest = 0;
public:
Numbers(int, int[]);
void showHighest(void)
{
cout << largest << endl;
}
};
Numbers::Numbers(int size, int arr[])
{
for (int i = 0; i < size; i++)
{
if (arr[i] > largest)
{
largest = arr[i];
}
}
}
int main(void)
{
int arrays[N], total;
cout << "How many elements? (starts from zero) ";
cin >> total;
for (int i = 0; i < total; i++)
{
cout << "Element " << i << ": ";
cin >> arrays[i];
}
Numbers n(total, arrays);
n.showHighest();
return 0;
}
Output
How many elements? (starts from zero) 3
Element 0: 12
Element 1: 16
Element 2: 11
16
Note: I've initialized a constant number of maximum elements, you can modify it. No vectors, etc. required to achieve so. You can either use your own values by removing the total and its followed statements and use only int arrays[<num>] = {...} instead. You're done!
Enjoy coding!
I suggest to use std::vector<int> or std::array<int>.
If you want initialize with custom values you can do std::vector<int> m_vec {0, 1, 2};
Thank you so much for your help. I was basically confused about how to use arrays in a constructor and use setters/getters for arrays in a class. But you all helped a lot. Thanks again.
Numbers(int arr[])
{
for (int i=0;i<=9;i++)
{
Arr[i]=arr[i];
}
Largest=Arr[0];
Smallest=Arr[0];
}
void Largest_Number()
{
header_top("Largest Number");
Largest=Arr[0]; //Using this so we make largest value as index zero
for (int i=0 ; i<=9 ; i++)
{
if(Arr[i]>Largest)
{
setLargest( Arr[i] );
}
}
cout<<"Largest Number: "<<getLargest()<<endl;
}
This question already has answers here:
Destructor being called twice when being explicitly invoked
(10 answers)
Closed 4 years ago.
So my code works fine when I do not call the destructor of the vector class. But it bugs out and my vector bugs out when I call the vector destructor. Can anyone explain to me why? From what I understand, adding the destructor line should not make any difference as I am just freeing the object once I used finished them. I'm compiling it online on the geekforgeeks ide if it helps.
#include <iostream>
#include <vector>
using namespace std;
//Function that converts from base 10 to another base given by user input
//And results are stored in the vector
int getRepresent(vector<int> &vec, int base, int num) {
if (num < base) {
vec.push_back(num);
return 1;
}
vec.push_back(num % base);
return 1 + getRepresent(vec, base, num / base);
}
//Compute the sum of squares of each digit
int computeSsd(int base, int num) {
vector<int> vec;
int len = getRepresent(vec, base, num);
int i;
int sum = 0;
for (i = 0; i < len; i++) {
sum += vec[i] * vec[i];
}
/*
for (auto x: vec) {
cout << x <<' ';
}
vec.~vector(); //the weird part that cause my vector to change once i add this line
*/
return sum;
}
int main() {
int size;
int i;
cin >> size;
for (i = 0; i < size; i++) {
int display;
int base;
int num;
cin >> display >> base >> num;
int ssd = computeSsd(base, num);
cout << display << ' ' << ssd << '\n';
}
return 0;
}
You shouldn't call the dsestructor yourself in this case*.
It will be called automatically, when the object goes out of scope.
What happened, is that you called the destructor yourself, then the destructor was called again automatically when the object went out of scope, which invoked Undefined Behavior (UB)! Think about it, when the destructor is called automatically, the object is already destructed!
*Is calling destructor manually always a sign of bad design?
I am trying to insert int array x to int *v. here is my code . please provide me with optimal solutions and the reason behind it.
there is an error in this line. Instead of copying array value its taking garbage value. line v1=x;
class vector
{
int *v;
int size;
public:
vector(int m)
{
v = new int[size = m];
for (int i = 0; i < size; i++)
v[i] = 0;
}
vector(int *a)
{
for (int i = 0; i < size; i++)
v[i] = a[i];
}
int operator *(vector &y)
{
int sum = 0;
for (int i = 0; i < size; i++)
sum += v[i] * y.v[i];
return sum;
}
void disp()
{
for (int i = 0; i < size; i++)
cout << v[i] << " ";
cout << "\n";
}
};
int main()
{
clrscr();
int x[3] = { 1,2,3 };
int y[3] = { 4,5,6 };
vector v1(3);
//v1.disp();
vector v2(3);
v2.disp();
v1 = x;
v1.disp();
//v2=y;
v2.disp();
int r = v1 * v2;
cout << "R = " << r;
getch();
return 0;
}
You forgot to add the assignment operator in your vector class:
vector & operator=(int *a)
{
for (int i = 0; i < size; i++)
v[i] = a[i];
return *this;
}
In the the line
v1=x;
May be, you are expecting it to invoke the second constructor which takes int* as argument. But it won't happen.
It can be seen as Type Conversion from Basic type to Class type. where we expect appropriate constructor will get invoked.
see http://www.hexainclude.com/basic-to-class-type-conversion/
But remember, Constructor will be invoked only once after the creation of object.
Here, in the line
vector v1(3);
the first constructor was already invoked. Then the line
v1=x;
won't invoke the second constructor now.
For every class, = operator is default overloaded . That is the reason why we can easily assign objects to one another.
Therefore, the line v1=x invokes default overloaded assignment = operator.
Here, it treats address of array x i.e., &x[0] as address of class object. As it is not address of vector class object
=> it results a Segmentation fault.
YOUR ANSWER
To assign int array to int pointer i.e., to the member variable int* v of the vector class,
overload assignment operator = inside the class .
or
Initialize the class object to array in first line itself. i.e.,
vector v1=x; // modify the class constructor to have size as a constant.
I am writing a simple header file that can make a dynamic 2d array and put 0 in the row and col, print the array, and delete the array.
In Debug when stepping through, the 2d array gets initialized, it puts 0 in there, however when my_array.Print_Array(); called, the compiler skips it.
when i try to print the array from the main file it fails. any help would be appreciate it.
HEADER FILE:
class twoD_Array
{
public:
int **Array;
int *col, *row;
int size_col, size_row;
twoD_Array(int, int);
~twoD_Array();
void Print_Array();
};
twoD_Array::twoD_Array(int size_c, int size_r)
{
size_col = size_c;
size_row = size_r;
Array = new int *[size_col];
for (int i = 0; i < size_col; i++)
{
Array[i] = new int[size_row];
for (int j = 0; j < size_row; j++)
Array[i][j] = 0;
}
}
void twoD_Array::Print_Array()
{
for (int y_i = 0; y_i<size_col; y_i++)
{
for (int x_i = 0; x_i<size_col; x_i++)
std::cout << Array[y_i][x_i];
std::cout << std::endl;
}
}
twoD_Array::~twoD_Array()
{
for (int i = 0; i < size_row; i++)
delete[] Array[i];
delete[] Array;
}
Main File:
#include "stdafx.h"
#include <iostream>
#include "2D_Array.h"
int main()
{
int x, y;
std::cout << "how many x variables?" << std::endl;
std::cin >> x;
std::cout << "how many y variables?" << std::endl;
std::cin >> y;
twoD_Array my_array(x, y);
my_array.Print_Array();
return 0;
}
You are using the local variables in the constructor, but you're using the member variables in the Print_Array method, which are not initialized. You need to initialize the member variables size_col and size_row in the constructor where they are provided. Also, another thing to point out, in Print_Array method, you are using size_col instead of size_row for the x_i loop, which looks like a logical error.
I've rewritten the constructor and Print_Array to look this way:
twoD_Array::twoD_Array(int size_col, int size_row)
{
this->size_col = size_col;
this->size_row = size_row;
Array = new int *[size_col];
for (int i = 0; i < size_col; i++)
{
Array[i] = new int[size_row];
for (int j = 0; j < size_row; j++)
{
Array[i][j] = 0;
}
}
}
void twoD_Array::Print_Array()
{
for (int y_i = 0; y_i < size_col; y_i++)
{
for (int x_i = 0; x_i < size_row; x_i++)
{
std::cout << Array[y_i][x_i];
}
std::cout << std::endl;
}
}
In
twoD_Array::twoD_Array(int size_col, int size_row)
int size_col defines a new temporary and local to the constructor variable named size_col. This size_col has nothing to do with the member variable size_col and because they have the same name the local variable hides the member variable.
End result is in Print_Array the member variable size_col has not been set to anything, so Crom only knows what will happen.
Solution:
Set the member variable:
twoD_Array::twoD_Array(int col, int row): size_col(col), size_row(row)
The colon (:) tells the compiler that a Member Initializer List is coming. The Member Initializer List allows you to initialize class member before entering the body of the constructor. This is very important when you have a member variable that has no default constructor or requires expensive initialization you don't want to be forced to do twice. It' also allows you to initialize a base class.
Note that I also changed the names of the parameters to prevent future confusion.
And while we are here, let's head off what is likely to be OP's next problem by asking What is The Rule of Three? Seriously. Click the link. Save you a ton of debugging.
I am trying to do a simple thing but suddenly stuck in between .
Here in my code I am trying to call a constructor in which i would only pass the length, my first constructor initializes an array of size = length with all elements 0.
Then i am passing the array to the constructor to give the previously defined array its values
here is the sample :
class myvector
{
int *arr;
int length;
public :
myvector(int);
myvector(int *);
};
myvector :: myvector (int len)
{
arr = new int [length = len];
for ( int i=0;i< length;i++)
{
arr[i] = 0;
}
}
myvector :: myvector ( int *ex)
{
for ( int i=0;i< length;i++)
{
cout << ex[i] << " " << length <<" ";
arr[i] = ex[i];
cout << arr[i]<< " ";
}
}
int main()
{
myvector v1(5);
int x[5] = {2,3,4,45,6};
v1 = x;
}
Here in my second constructor length which was defined in first constrcutor lost its values , also array arr loses its values
Did I do something ?
Please elaborate me on this
I don't think you quite get what the circumstances are in which constructors are invoked. The line v1 = x doesn't put the values into the memory allocated during the first constructor call. Rather, it constructs a new myvector (using the second constructor) and copies it into v1. The stuff you do during the first constructor call is lost.
It sounds like you want to define an assignment operator, not a constructor taking an int* argument. Also, in general you should declare single-argument constructors as explicit to prevent this sort of thing from happening accidentally.
First of all, when specifying a size of array like this, you should provide a constant value. See here. And I really encourage you to use std::vector<> for such tasks.
But anyway, like Sneftel mentioned, seems like you want to define an assignment operator (see here for more information about overloading operators in tasks like yours).
Here how I'd implemented it(NOT and ideal solution, but it works, and gives a basic idea):
#include <iostream>
using namespace std;
class myvector
{
int *arr;
int length;
public :
//I completely removed second constructor
myvector(int);
~myvector();
void operator=(const int* otherArray);
void printArray();
};
myvector::myvector (int len)
{
length = len;
arr = new int[length]; // This is the way, how you can handle a non constant array sizes
for ( int i=0;i< length;i++)
{
arr[i] = 0;
}
}
void myvector::printArray()
{
for(unsigned i = 0; i < length; ++i)
cout<<"arr["<<i<<"] = "<<arr[i]<<endl;
}
//Here's an overloaded '=' operator.
//int x[5] = {2,3,4,45,6};
//myvector v;
//v = x; - here this function is called
void myvector::operator=(const int* otherArray)
{
for(unsigned i = 0; i < length; ++i)
arr[i] = otherArray[i];
}
myvector::~myvector()
{
delete []arr; // You should ALWAYS delete what you allocated with new
}
int main()
{
myvector v1(5);
int x[5] = {2,3,4,45,6};
v1 = x;
v1.printArray();
}