void main()
{
int a2[] = {1};
int a3[] = {1, 2};
int a4[] = {1, 2, 3};
int a5[] = {1, 2, 3, 4};
int a6[] = {3, 3, 4, 4};
int a7[] = {3, 2, 3, 4};
int a8[] = {4, 1, 2, 3};
int a9[] = {1, 1};
araay (a6,3);
}
void araay(int arg[], int length)
{
int sumEven = 0;
int sumOdd = 0;
for (int i=0; i<=length; i++)
{
if (arg[i]%2 == 0)
sumEven += arg[i];
else
sumOdd += arg[i];
}
cout << sumOdd - sumEven;
}
This my code for that takes an array of integers as an argument and returns a value based on the sums of the even and odd numbers in the array but the error i am getting is
araay identifier is not found
Simply put
void araay(int arg[], int length);
before main() { ... to declare your function.
In your code you have only definition of your function araay(...). You need to add declaration of this function before main() to provide information about function aray(...) for main().
You always must put at least declaration of function before main() to be able use it in main(). Declaration of function before main() can be combine with its definition.
You need to declare your function above main before you call it. Or you can put the whole function above the main and its working fine.
void araay(int arg[], int length)
{
int sumEven = 0;
int sumOdd = 0;
for (int i=0; i<=length; i++)
{
if (arg[i]%2 == 0)
sumEven += arg[i];
else
sumOdd += arg[i];
}
cout << sumOdd - sumEven;
}
int main()
{
....
}
Related
it's a test from some company.
overload & to return sum of int[][]
main()
{
int arr[5][5];
// initialization
cout << &arr; // output the sum of arr
}
I try this code but returns compile error:
long operator &(int** arr)
{
// return the sum of arr
}
error: the argument should contains class type or enumeration
currently I understand this error, but how to overload operator for buildin types?
Example of overloading operator &, only for educational use!
I would NOT recommend overloading operator& in any serious code.
#include <iostream>
struct my_array
{
my_array()
{
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
values[i][j] = 1 + i + j;
}
}
}
int operator&()
{
int sum = 0;
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
sum += values[i][j];
}
}
return sum;
}
private:
int values[5][5]{};
};
int main()
{
my_array m;
int sum = &m;
std::cout << sum;
}
I think interviewer may want you to overload operator<< actually, not & and the solution could then be like below. See it live here.
#include <iostream>
using int5x5_t = int[5][5];
std::ostream& operator<<(std::ostream& os, const int5x5_t* parr) {
long sum = 0;
for (auto& vec : *parr) {
for (auto val : vec) {
sum += val;
}
}
return os << sum;
}
int main() {
int arr[5][5] = {
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4},
{0, 1, 2, 3, 4}
};
std::cout << &arr << std::endl;
}
I'm having an issue in my program with my insertElement() function. What I had intended insertElement to do is to take the index from the prototype and move all the values to the right, including the value on that index, to the right ONCE. So, If I were to have my array {1, 2, 3, 4} and I wanted to insert the value "10" at the index "2", the resulting array would be {1, 2, 10, 3, 4}.
I know I'd have to tweak my insertElement() function to fix this issue, but I'm not sure where to start, could anybody give me a hand? Here is my code:
#include <iostream>
using namespace std;
const int CAPACITY = 20;
void displayArray(int array[], int numElements)
{
for (int i = 0; i < numElements; i++)
cout << array[i] << " ";
cout << endl;
}
bool insertElement(int array[], int& numberElements, int insertPosition, int insertTarget)
{
int p = 0;
int j = 1;
int arrayPositionFromLast = (numberElements-1);
if (numberElements>=CAPACITY)
{
cout << "Cannot insert an element, array is full." << endl;
return false;
} else {
for (int i=arrayPositionFromLast; i>insertPosition; i--)
{
array[arrayPositionFromLast-p]=array[arrayPositionFromLast-j];
p++;
j++;
}
array[insertPosition] = insertTarget;
}
return true;
}
int main()
{
int array[6] = {1, 2, 3, 4, 5, 6};
int numArrayElements = 6;
int endOfArrayValue, insertedValue, insertedValuePosition;
cout << "Enter a value and a position to insert: ";
cin >> insertedValue >> insertedValuePosition;
insertElement(array, numArrayElements, insertedValuePosition, insertedValue);
displayArray(array, numArrayElements);
}
first you should define your array with CAPACITY
int array[CAPACITY] = {1, 2, 3, 4, 5, 6};
You can move your data with memmove.
if (numberElements>=CAPACITY)
{
cout << "Cannot insert an element, array is full." << endl;
return false;
} else {
memmove(array + insertPosition+ 1, array + insertPosition, (numberElements - insertPosition) * sizeof (int));
array[insertPosition] = insertTarget;
}
I'm trying to reverse an array and I think the for loop is wrong but everyone I've shown it to doesn't see a problem.
#include <iostream>
void reverse() {
int temp;
const int size = 9;
int arr[size] = {1, 4, 10, 16, 34, 7, 8, 9, 11};
for (int i=0; i <size-1; i++){
for (int j=size-1; j>=0; j--){
arr[j]= temp;
arr[i] = temp;
}
}
for(int x= 0; x<size; x++){
std::cout<<arr[x]<< " ";
}
}
int main () {
reverse();
return 0;
}
I suggest you use an algorithm of the std, namely std::reverse. You don't have to invent an algorithm, that reverts an array.
Thus your code is reduced to
#include <iostream>
#include <array>
#include <algorithm>
void reverse() {
std::array<int, 9> arr{1, 4, 10, 16, 34, 7, 8, 9, 11};
std::reverse(arr.begin(), arr.end());
for (const auto& item : arr) {
std::cout << item << " ";
}
}
int main() {
reverse();
return 0;
}
Choosing proper container and algorithm form the standard library will significantly enhance the quality (in terms of bugs, readability, ...) and speed up you development. Furthermore, in most cases the runtime of your program will be shorter as well.
#include<iostream>
using namespace std;
void reverse() {
int temp;
const int size = 9;
int arr[size] = {1, 4, 10, 16, 34, 7, 8, 9, 11};
for (int i=0, j =size-1; i <j ; i++, j--)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
for(int x= 0; x<size; x++){
std::cout<<arr[x]<< " ";
}
}
int main () {
reverse();
return 0;
}
The variable 'temp' in your code is not initialised.
// I think this is what you are trying to do.
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
This would be faster. You just traverse half of the array and switch the elements don't do n² traversion, this is definitely not needed here.
void reverse() {
const int size = 9;
int arr[size] = {1, 4, 10, 16, 34, 7, 8, 9, 11};
int back = size - 1;
int temp;
for( int i = 0; i < size / 2; ++i)
{
temp = arr[i];
arr[i] = arr[back];
arr[back--] = temp;
}
for( auto n : arr )
std::cout << n << " ";
std::cout << std::endl;
}
to your code:
void reverse() {
int temp;
const int size = 9;
int arr[size] = {1, 4, 10, 16, 34, 7, 8, 9, 11};
for (int i=0; i <size-1; i++){
for (int j=size-1; j>=0; j--){ // here every element is set to
// temp, but temp isn initialized so they are set to a random value.
// Try setting temp to 0 and look what happens your whole array should
// become 0.
arr[j]= temp;
arr[i] = temp;
}
}
for(int x= 0; x<size; x++){
std::cout<<arr[x]<< " ";
}
}
check this one to reverse ur array..
#include <iostream>
using namespace std;
void reverse() {
int temp;
const int size = 9;
int arr[size] = {1, 4, 10, 16, 34, 7, 8, 9, 11};
for (int i=8; i>=0; i--){
// cout<<arr[i]<< " ";
std::cout<<arr[i]<< " ";
}
}
int main () {
reverse();
return 0;
}
I am doing the program in which i am checking that if the array is balance or not like the array
int a5[] = {2, 1, 4, 3}; // balance array because i got even number on even position so program return 1
int a5[] = {3, 1, 4, 3}; // un balance array because i got odd number on even position so program return 0
This is my program which i am trying
int araay(int arg[], int length);
int main()
{
int a6[] = {3, 3, 4, 4};
int a7[] = {2, 2, 3, 4};
int a8[] = {4, 1, 2, 3};
int a9[] = {1, 1};
araay (a7,sizeof(a7));
}
int araay (int arg[], int length)
{
int sumEven = 0;
int sumOdd = 0;
for (int i=0; i<=length; i=i+2)
{
if (arg[i]%2 == 0)
{
sumEven++;
}
else
sumOdd++;
}
for (int i=1; i<=length; i=i+2)
{
if (arg[i]%2 == 0)
{
sumEven++;
}
else
sumOdd++;
}
return 0;
}
in return it always return me 00000 something like zero everytime
Following may help: (http://ideone.com/NttqbY)
bool is_balanced(const std::vector<std::size_t>& v)
{
for (std::size_t i = 0; i != v.size(); ++i) {
if ((i % 2) != (v[i] % 2)) {
return false;
}
}
return true;
}
Thanks all for your comments and help
This is what i tried
int araay(int arg[], int length);
int main()
{
int a6[] = {3, 3, 4, 4};
int a7[] = {2, 3, 2, 3};
int a8[] = {4, 1, 2, 3};
int a9[] = {1, 1};
araay (a7,3);
}
int araay (int arg[], int length)
{
int sumEven = 0;
int sumOdd = 0;
for (int i=0; i<=length; i+=2)
{
if (arg[i]%2 != 0)
{
cout<<"unbalanced"<<endl;
// return 0;
}
else
{
sumEven++;
}
}
for (int i=1; i<=length; i=i+2)
{
if (arg[i]%2 == 0)
{
cout<<"unbalanced"<<endl;
sumEven++;
}
else
{
sumOdd++;
// return 0;
}
}
return 0;
}
but the answer of #jarod is looking more suitable and easy
This is what you can do
#include <iostream>
int check(int arg[])
{
for (auto i = 0; i < sizeof(arg); ++i)
{
if ((i % 2) != (arg[i] % 2))
{
return 0;
}
}
return 1;
}
void main()
{
int a[] = { 1, 2, 3, 4 };
int b[] = { 2, 3, 4, 5 };
int c[] = { 2, 2, 3, 3 };
std::cout << "a = " << check(a) << std::endl;
std::cout << "b = " << check(b) << std::endl;
std::cout << "c = " << check(c) << std::endl;
getchar();
}
Why does the for_each call on functor doesn't update sum::total at the end?
struct sum
{
sum():total(0){};
int total;
void operator()(int element)
{
total+=element;
}
};
int main()
{
sum s;
int arr[] = {0, 1, 2, 3, 4, 5};
std::for_each(arr, arr+6, s);
cout << s.total << endl; // prints total = 0;
}
for_each takes the functor by value - so it is copied. You can e.g. use a functor which is initialized with a pointer to an external int.
struct sum
{
sum(int * t):total(t){};
int * total;
void operator()(int element)
{
*total+=element;
}
};
int main()
{
int total = 0;
sum s(&total);
int arr[] = {0, 1, 2, 3, 4, 5};
std::for_each(arr, arr+6, s);
cout << total << endl; // prints total = 15;
}
Or you can use the return value from for_each
struct sum
{
sum():total(0){};
int total;
void operator()(int element)
{
total+=element;
}
};
int main()
{
sum s;
int arr[] = {0, 1, 2, 3, 4, 5};
s = std::for_each(arr, arr+6, s);
cout << s.total << endl; // prints total = 15;
}
for_each receives a copy of your functor by value. Even after that, it's free to copy it, but does return a copy.
OTOH, you're simply trying to re-invent std::accumulate, which will do the job much more easily:
int total = std::accumulate(arr, arr+6, 0);
cout << total << endl;
Because the s which you pass to the for_each is by value. for_each accepts it by value!
In C++0x, you can solve this problem with for_each as,
int sum = 0;
std::for_each(arr, arr+6, [&](int n){ sum += n; });
std::cout << sum ;
Output:
15
Demo at ideone : http://ideone.com/s7OOn
Or you can simple write in the std::cout itself:
std::cout<<std::for_each(arr,arr+6,[&](int n)->int{sum += n;return sum;})(0);
Run : http://ideone.com/7Hyla
Note such different syntax is okay for learning purpose, as to how std::for_each works, and what it returns, but I would not recommend this syntax in real code. :-)
In C++, you can write user-defined conversion function in the functor as,
struct add
{
int total;
add():total(0){};
void operator()(int element) { total+=element; }
operator int() { return total ; }
};
int main()
{
int arr[] = {0, 1, 2, 3, 4, 5};
int sum = std::for_each(arr, arr+6, add());
std::cout << sum;
}
It's slightly different version from Erik second solution : http://ideone.com/vKnmA
This happens due to std::for_each requires the functor to be passed by value .
A workaround for your solution:
struct sum
{
sum():total(0){};
int total;
sum(sum & temp)
{
total = temp.total;
}
void operator()(int element)
{
total+=element;
}
};
int main()
{
sum s;
int arr[] = {0, 1, 2, 3, 4, 5};
s = std::for_each(arr, arr+6, s); // result of for_each assigned back to s
cout << s.total << endl; // prints total = 0;
}
std::ref() is also another option if you want your object state gets updated after std::for_each
struct Sum
{
int total = 0;
void operator()(int i) { total += i; }
};
int main()
{
int arr[] = { 0, 1, 2, 3, 4, 5 };
Sum obj1;
Sum t1 = std::for_each(arr, arr + 6, obj1); // t1.total = 15
// (Note: obj1.total = 0 bcos it is passed by value)
Sum obj2;
std::for_each(arr, arr + 6, std::ref(obj2)); // obj2.total = 15 (NO copy)
Sum t2 = std::for_each(arr, arr + 6, Sum()); // t2.total = 15
}