I have to write a program that creates an array and then counts the total number of possible additions that can be made between the elements. (It doesn’t matter what is actually in the element just how many combinations can be made). i.e a 2*2 array should have 10 possible additions. So far my code looks like
#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <vector>
using namespace std;
int n;
int count;
int main()
{
cout<<"\nEnter Number of rows and columns you wish to calculate the
possible number additions. \n;
cin >> n;
if (!cin)
{
do
{
n = 0;
cout << "That is not a valid number, please enter another. \n";
cin >> n;
}
while (!cin);
}
vector<vector<int> > matrix(n);
for ( int i = 0 ; i < n ; i++ )
{
matrix[i].resize(n);
}
for(int i =1; i < n^2; ++i)
{
count = count + i;
}
cout << count;
return (0);
}
Could you try and explain exactly what you’re trying to do? what do you mean “counts the total number of possible additions that can be made between the elements”? why do you need to create an array if the answer is independent on the actual elements (otherwise I don’t understand your 2*2 example).
One problem that I can spot in your code is that you mistake n^2 to be equal to n*n but the ‘^’ operator is actually a XOR bit-wise operator.