An array of 'N' integers, it is necessary to calculate the next equation:
у = х1 * (х1 + х2) * (х1 + х2 + х3) * ... * (x1 + ... + xN)
I have two questions:
Is there a better way to find the solution y?
How to generate random number except of 0?
Code:
srand(time(NULL));
const int size = 10;
int arr[size];
int pro=1;
for (int i = 0; i < size; i++)
{
arr[i] = rand() % 10;
}
for (int i = 0; i < size; i++)
{
cout << arr[i] << ' ';
}
cout << endl;
for (int i = 0; i < size; i++)
{
int sum = 0;
for (int j = 0; j <= i; j++)
{
sum = arr[j];
}
pro *= sum;
}
cout << pro << endl;
How to generate random number except 0?
arr[i] = rand() % 9 + 1; // rando number in range 1 .. 9
Is there a better way to find the solution y?
long pro = 1;
long sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i]; // i==0: x1, i==1: x1+x2, i==3: x1+x2+x3, ....
pro *= sum;
}
This should be your code:
srand(time(NULL));
const int size = 10;
int arr[size];
for (int i = 0; i < size; i++)
arr[i] = rand() % 9 + 1;
for (int i = 0; i < size; i++)
cout << arr[i] << ' ';
cout << endl;
long pro = 1;
long sum = 0;
for (int i = 0; i < size; i++)
{
sum += arr[i];
pro *= sum;
}
cout << pro << endl;
Related
I have this task:
A user inputs a number N and you have to output this pyramid:
0
101
21012
.......
N.21012.N
For N=5 it will be :
0
101
21012
3210123
432101234
54321012345
I managed to only get it working for N<10 with this code:
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
for (int j = 0; j < n - i; j++)
cout << " ";
int dir = -1;
for (int k = i; k <= i; k += dir) {
cout << k;
if (k == 0)
dir = 1;
}
cout << endl;
}
For N=10 it will look like this :
0
101
21012
3210123
432101234
54321012345
6543210123456
765432101234567
87654321012345678
9876543210123456789
10987654321012345678910
After the answers I settled on this :
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
int n, spaces;
string number;
cin >> n;
if (n < 10)
spaces = n;
else
{
spaces = 9;
int pwr = 0, k = n;
while (k > 9)
{
pwr++;
k /= 10;
}
for (int i = 1; i < pwr; i++)
{
spaces += pow(10, i) * 9 * (i + 1);
}
spaces += (n - pow(10, pwr) + 1) * (pwr + 1);
}
// cout << spaces << endl;
for (int i = 0; i < n + 1; i++)
{
for (int j = i; j > -1; j--)
number += to_string(j);
int len = number.length() - 1;
for (int j = 0; j < spaces - len; j++)
cout << " ";
for (int j = 1; j <= i; j++)
number += to_string(j);
cout << number << endl;
number.clear();
}
cout << endl;
return 0;
}
int padding(int n) {
constexpr auto singleDigitNumbersCount = 9;
constexpr auto doubleDigitNumbersCount = 90; // from 10 to 99
if (n < 10) return n;
if (n < 100) return 2*n - singleDigitNumbersCount;
return 3*n - doubleDigitNumbersCount - 2*singleDigitNumbersCount;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n + 1; i++) {
std::cout << std::string(padding(n) - padding(i), ' ');
for (int k = i; k >= 0; k--) {
cout << k;
}
for (int k = 1; k <= i; k++) {
cout << k;
}
cout << '\n';
}
return 0;
}
https://godbolt.org/z/EEaeWEvf4
I made this a bit ago Compiler Explorer
Not sure if that'd help 🤔
Here is the working code:
#include <string>
#include <iostream>
using namespace std;
#define MAX_SPACE 50
int main()
{
int n;
cin >> n;
string output = "";
for (int i = 0; i < n + 1; i++)
{
for (int k = i; k >= 0; k--) {
output += to_string(k);
}
for (int k = 1; k <= i; k++) {
output += to_string(k);
}
for (uint8_t i = 0, max = MAX_SPACE - output.length() / 2.00; i < max; i++) // Print max spaces minus the integer length divided by 2
{
cout << " ";
}
cout << output << endl; // Print number
output = "";
}
return 0;
}
this is the code that is have i am trying to get the take the for loops that i have and condense them
into a loop but i am not sure how. If there is anyone that can help me out i would appreciate it. The fist set of for loops are taking the inverse and then it is muiltiplying through the positions of the array. Right now the for loops are going through on position for each loop and i know there is a better way but i cant think of how to do it.
using namespace std;
#include<iostream>
#include<fstream>
#include<iomanip>
#include<Windows.h>
// User-defined function declarations (prototypes)
void readit();
void calcit(float[5][6], float[5][6]);
void writeit(float [5][6], float[5][6], float[5]);
// Declaration and definition of the main()
int main()
{
readit();
return 0;
}
void readit()
{
// Local variable declarations
float origarray[5][6], reducedarray[5][6];
// Filestream declaration and error trap
ifstream infile("C:\\EGR111\\rowechelondata.txt");
if(!infile)
{
cout << "There is no file, or the filestream is corrupted. Correct the problem and "
<< "try again!";
Sleep(2000);
exit(0);
}
// File read. 'i' is row index and 'j' is column index.
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
infile >> origarray[i][j];
reducedarray[i][j] = origarray[i][j];
}
}
calcit(origarray, reducedarray);
}
void calcit(float origarray[5][6], float reducedarray[5][6])
{
// Local variable declaration
float roots[5] = {};
cout << setprecision(4) << endl;
// Multiply first row by its leading coefficient, such that the result is '1'
for(int i = 0; i < 1; i++)
{
for(int j = 0; j < 6; j++)
{
reducedarray[i][j] = origarray[i][j] * (1.0 / origarray[0][0]);
}
}
// Multiply second row by its leading coefficient, such that the result is '1'
for(int i = 1; i < 2; i++)
{
for(int j = 0; j < 6; j++)
{
reducedarray[i][j] = origarray[i][j] * (1.0 / origarray[1][0]);
}
}
// Multiply third row by its leading coefficient, such that the result is '1'
for(int i = 2; i < 3; i++)
{
for(int j = 0; j < 6; j++)
{
reducedarray[i][j] = origarray[i][j] * (1.0 / origarray[2][0]);
}
}
// Multiply fourth row by its leading coefficient, such that the result is '1'
for(int i = 3; i < 4; i++)
{
for(int j = 0; j < 6; j++)
{
reducedarray[i][j] = origarray[i][j] * (1.0 / origarray[3][0]);
}
}
// Multiply fifth row by its leading coefficient, such that the result is '1'
for(int i = 4; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
reducedarray[i][j] = origarray[i][j] * (1.0 / origarray[4][0]);
}
}
// Subtract the first row of the reduced array into the subsequent rows
for(int i = 1; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] - reducedarray[0][j];
}
}
// Multiply array position [1][1] by its leading coefficient, such that the result is '1'
for(int i = 1; i < 2; i++)
{
for(int j = 1; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[1][1]);
}
}
// Multiply array position [2][1] by its leading coefficient, such that the result is '1'
for(int i = 2; i < 3; i++)
{
for(int j = 1; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[2][1]);
}
}
// Multiply array position [3][1] by its leading coefficient, such that the result is '1'
for(int i = 3; i < 4; i++)
{
for(int j = 1; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[3][1]);
}
}
// Multiply array position [4][1] by its leading coefficient, such that the result is '1'
for(int i = 4; i < 5; i++)
{
for(int j = 1; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[4][1]);
}
}
// Subtract the second row of the reduced array into the subsequent rows
for(int i = 2; i < 5; i++)
{
for(int j = 1; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] - reducedarray[1][j];
}
}
// Multiply array position [2][2] by its leading coefficient, such that the result is '1'
for(int i = 2; i < 3; i++)
{
for(int j = 2; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[2][2]);
}
}
// Multiply array position [3][2] by its leading coefficient, such that the result is '1'
for(int i = 3; i < 4; i++)
{
for(int j = 2; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[3][2]);
}
}
// Multiply array position [4][2] by its leading coefficient, such that the result is '1'
for(int i = 4; i < 5; i++)
{
for(int j = 2; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[4][2]);
}
}
// Subtract the third row of the reduced array into the subsequent rows
for(int i = 3; i < 5; i++)
{
for(int j = 2; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] - reducedarray[2][j];
}
}
// Multiply array position [3][3] by its leading coefficient, such that the result is '1'
for(int i = 3; i < 4; i++)
{
for(int j = 3; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[3][3]);
}
}
// Multiply array position [4][3] by its leading coefficient, such that the result is '1'
for(int i = 4; i < 5; i++)
{
for(int j = 3; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[4][3]);
}
}
// Subtract the fourth row of the reduced array into the subsequent row
for(int i = 4; i < 5; i++)
{
for(int j = 3; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] - reducedarray[3][j];
}
}
// Multiply array position [4][4] by its leading coefficient, such that the result is '1'
for(int i = 4; i <= 4; i++)
{
for(int j = 4; j < 6; j++)
{
reducedarray[i][j] = reducedarray[i][j] * (1.0 / reducedarray[4][4]);
}
}
// Back solve to assign roots for each row
for(int i = 4; i >= 0; i--)
{
for(int j = 5; j >= 0; j--)
{
roots[i] = reducedarray[4][5];
roots[4] = roots[i];
}
}
for(int i = 3; i >= 0; i--)
{
for(int j = 5; j >= 0; j--)
{
roots[i] = reducedarray[3][5] - (reducedarray[3][4] * roots[4]);
roots[3] = roots[i];
}
}
for(int i = 2; i >= 0; i--)
{
for(int j = 5; j >= 0; j--)
{
roots[i] = reducedarray[2][5] - (reducedarray[2][4] * roots[4]) -
(reducedarray[2][3] * roots[3]);
roots[2] = roots[i];
}
}
for(int i = 1; i >= 0; i--)
{
for(int j = 5; j >= 0; j--)
{
roots[i] = reducedarray[1][5] - (reducedarray[1][4] * roots[4]) -
(reducedarray[1][3] * roots[3]) - (reducedarray[1][2] * roots[2]);
roots[1] = roots[i];
}
}
for(int i = 0; i >= 0; i--)
{
for(int j = 5; j >= 0; j--)
{
roots[i] = reducedarray[0][5] - (reducedarray[0][4] * roots[4]) -
(reducedarray[0][3] * roots[3]) - (reducedarray[0][2] * roots[2]) -
(reducedarray[0][1] * roots[1]);
roots[0] = roots[i];
}
}
writeit(origarray, reducedarray, roots);
}
void writeit(float origarray[5][6], float reducedarray[5][6], float roots[5])
{
cout << "The Original Array" << endl << endl;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
cout << setw(6) << origarray[i][j] << setw(4) << "";
}
cout << endl << endl;
}
cout << endl << endl;
cout << "The Reduced Array" << endl << endl;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
cout << setw(6) << reducedarray[i][j] << setw(4) << "";
}
cout << endl << endl;
}
cout << endl << endl;
cout << "The Roots of the equations are: " << endl << endl;
cout << setw(6) << "A = " << setw(6) << roots[0] << endl << endl;
cout << setw(6) << "B = " << setw(6) << roots[1] << endl << endl;
cout << setw(6) << "C = " << setw(6) << roots[2] << endl << endl;
cout << setw(6) << "D = " << setw(6) << roots[3] << endl << endl;
cout << setw(6) << "E = " << setw(6) << roots[4] << endl << endl;
cout << endl << endl;
}
Where m=number of columns in matrix and n=number of rows in matrix.
Just the needed segment:
void calcit(float origarray[5][6], float reducedarray[5][6])
{
float roots[5] = {};
int n=5,m=6;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
reducedarray[i][j]=origarray[i][j]*(1.0/origarray[i][0]);
}
}
for(int i = 1; i < 5; i++){
for(int j = 0; j < 6; j++){
reducedarray[i][j] -= reducedarray[0][j];
}
}
int num=1;
for(int i=1;i<m;i++){
for(int j=num;j<n;j++){
for(int k=i;k<m;k++)
reducedarray[j][k] = reducedarray[j][k] * (1.0 / reducedarray[j][i]);
}
for(int j=num+1;j<n;j++){
for(int k=num;k<m;k++){
reducedarray[j][k] -= reducedarray[i][k];
}
}
num++;
}
for(int i=n-1;i>=0;i--){
float ans=reducedarray[i][m-1];
for(int j=m-2;j>=i+1;j--){
ans-=(reducedarray[i][j]*roots[j]);
}
roots[i]=ans;
}
writeit(origarray, reducedarray, roots);
}
Whole code:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<Windows.h>
using namespace std;
// User-defined function declarations (prototypes)
void readit();
void calcit(float[5][6], float[5][6]);
void writeit(float [5][6], float[5][6], float[5]);
// Declaration and definition of the main()
int main()
{
readit();
return 0;
}
void readit()
{
// Local variable declarations
float origarray[5][6], reducedarray[5][6];
// Filestream declaration and error tra
cout << setprecision(4) << endl;
// File read. 'i' is row index and 'j' is column index.
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
cin >> origarray[i][j];
reducedarray[i][j] = origarray[i][j];
}
}
calcit(origarray, reducedarray);
}
void calcit(float origarray[5][6], float reducedarray[5][6])
{
float roots[5] = {};
int n=5,m=6;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
reducedarray[i][j]=origarray[i][j]*(1.0/origarray[i][0]);
}
}
for(int i = 1; i < 5; i++){
for(int j = 0; j < 6; j++){
reducedarray[i][j] -= reducedarray[0][j];
}
}
int num=1;
for(int i=1;i<m;i++){
for(int j=num;j<n;j++){
for(int k=i;k<m;k++)
reducedarray[j][k] = reducedarray[j][k] * (1.0 / reducedarray[j][i]);
}
for(int j=num+1;j<n;j++){
for(int k=num;k<m;k++){
reducedarray[j][k] -= reducedarray[i][k];
}
}
num++;
}
for(int i=n-1;i>=0;i--){
float ans=reducedarray[i][m-1];
for(int j=m-2;j>=i+1;j--){
ans-=(reducedarray[i][j]*roots[j]);
}
roots[i]=ans;
}
writeit(origarray, reducedarray, roots);
}
void writeit(float origarray[5][6], float reducedarray[5][6], float roots[5])
{
cout << "The Original Array" << endl << endl;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
cout << setw(6) << origarray[i][j] << setw(4) << "";
}
cout << endl << endl;
}
cout << endl << endl;
cout << "The Reduced Array" << endl << endl;
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 6; j++)
{
cout << setw(6) << reducedarray[i][j] << setw(4) << "";
}
cout << endl << endl;
}
cout << endl << endl;
cout << "The Roots of the equations are: " << endl << endl;
for(int i=0;i<5;i++){
cout<<(char)('A'+i)<<" = "<<setw(6)<<roots[i]<<endl;
}
}
Please, kindly, check if it is working. If it does not work, please notify me in the comments of this post.
Also, i'm not sure why are you using double endl (cout<<endl<<endl;),there is an alternative to this: cout<<"\n\n"; (note that this doesn't flush the output buffer)
Here is my driver
int square[2][2] = {
{1,2},
{3,4}
};
matrixMulti(square, 2);
Here is my function
void matrixMulti(const int a[][2], const int rows) {
int b[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
b[i][j] += a[i][k] * a[k][j];
}
}
}
cout << "new matrix " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << b[i][j] << " ";
}
cout << endl;
}
}
The output is:
new matrix
-858993453 -858993450
-858993445 -858993438
I am confused as to why it is printing the memory addresses rather then the values stored in them and what can be done to get it to print the values rather than the memory address.
You wrote:
int b[2][2];
and then
b[i][j] += ...
Where did you initialize b ?
int b[2][2] = {};
or
int b[2][2] = {0,0};
Should do the job.
Garbage values are being printed, not the address of the array.
Here is why.
In this snippet of code
void matrixMulti(const int a[][2], const int rows) {
int b[2][2];
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
b[i][j] += a[i][k] * a[k][j];
}
}
}
You allocated space for int b[2][2], but you do not initialize it to a value. This would be fine except in the for-loop body.
b[i][j] += a[i][k] * a[k][j]; <=> b[i][j] = b[i][j] + a[i][k] * a[k][j];
you are referencing the value of b[i][j] before it is initialized. In other words you say b[i][j] is equal to b[i][j] + a[i][k] * a[k][j], but you can't do this as, the value of b[i][j] is unknown.
The solution to this is to initialize int b[2][2] = {{0,0},{0,0}};
Hey I am beginner at C++ programming. I have made a program that is meant to add two 2D arrays together. However, The program outputs the values until the program crashes. Can someone help me to identify the problem?
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 1; i <= 10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
// We are able to treat the individual columns as arrays
for (int i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
// Declare a multidimensional array on the heap
int **b = new int*[10];
// need to allocate all members individually
for (int i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
// Set the values of b
for (int i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 1; j <= 10; ++j)
{
cout << c[i][j] << endl;
}
}
// Delete the multidimensional array - have to delete each part
for (int i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
I corrected your code.Now, It's working and program didn't crash. You can try it out.
#include<conio.h>
#include<iostream.h>
int main(int argc, char** argv)
{
int a[10][10], c[10][10], i, j;
for (i = 0; i <10; ++i)
{
for(j=0; j < 10; ++j)
{
a[i][j] = i * j;
}
}
//We are able to treat the individual columns as arrays;
for (i = 0; i < 10; ++i)
{
int *b = a[i];
for (int j = 0; j < 10; ++j)
{
cout << b[j] << " ";
}
cout << endl;
}
cout << "****" << endl;
//Declare a multidimensional array on the heap;
int **b = new int*[10];
//need to allocate all members individually
for (i = 0; i < 10; ++i)
{
b[i] = new int[10];
}
//Set the values of b
for ( i = 0; i < 10; ++i)
{
for (j = 0; j < 10; ++j)
{
b[i][j] = (i * 10) + j;
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0; j <10; ++j)
{
c[i][j] = a[i][j] + b[i][j];
}
}
for (i = 0; i < 10; ++i)
{
for (j = 0;j < 10; ++j)
{
cout << c[i][j] << " ";
}
cout<<endl;
}
// Delete the multidimensional array - have to delete each part
for (i = 0; i < 10; ++i)
{
delete[] b[i];
}
delete[] b;
return 0;
}
i need help, the code below does not work as it returns nothing when i run it. I'm trying to add big number so large that the numbers won’t be able to be represented in the standard C++ integer data structures
mission.cpp
void Big2Add(const char * num1, const char * num2, char * result)
{
string a = num1;
string b = num2;
int min = (a.length() < b.length() ? a.length():b.length());
int max = (a.length() < b.length() ? b.length():a.length());
int *n1 = new int[max];
int *n2 = new int[max];
for (unsigned int i=0; i < a.length(); i++)
{
n1[i] = a.at(a.length() - 1 -i) - 48;
}
cout << a << endl;
for (unsigned int i=0; i < b.length(); i++)
{
n2[i] = b.at(b.length()-1 -i) - 48;
}
cout << b << endl;
int carry = 0;
int* sum = new int[max];
int k=0;
for (k = 0; k < max; k++)
{
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ( (n1[k] + n2[k] + carry) >= 10)
carry = 1;
else carry = 0;
}
sum[max] = carry;
for (int j= max; j >= 0; j--)
{
*result = sum[j];
}
}
main.cpp
char result[10];
const char * num1 = "10";
const char * num2 = "10";
Big2Add(num1, num2, result);
cout << "Part 3" << endl;
cout << "The addition of " << num1 << " and " << num2 << " is " << result << endl;
cout << endl;
First obvious bug:
int* sum = new int[max];
...
sum[max] = carry;
You need to allocate max+1 in order to use position max.
Second and third obvious bugs:
*result = sum[j];
You forgot to advance result and you forgot to add '0'
Fourth, you forgot to null terminate the string.
Try:
for (int j= max; j >= 0; j--)
{
*(result++) = sum[j] + '0';
}
*result = 0;
ok it works now tks but my main problem now is that is that there is a extra zero in front of my answer if let say i do 10 plus 10 it will give me 010 or 100 with 10 it will give me 0110 but if i plus 99 with 99 it will give 198 y?
void Big2Add(const char * num1, const char * num2, char * result)
{
string a = num1;
string b = num2;
int max = (a.length() < b.length() ? b.length():a.length());
int *n1 = new int[max];
int *n2 = new int[max];
unsigned int i;
for (i=0; i < a.length(); i++)
{
n1[i] = a.at(a.length() - 1 -i) - 48;
}
for (int j = i; j < max; ++j)
{
n1[j] = 0;
}
for (i=0; i < b.length(); i++)
{
n2[i] = b.at(b.length()-1 -i) - 48;
}
for (int j = i; j < max; ++j)
{
n2[j] = 0;
}
int carry = 0;
int* sum = new int[max];
int k=0;
for (k = 0; k < max; k++)
{
sum[k] = (n1[k] + n2[k] + carry) % 10;
if ( (n1[k] + n2[k] + carry) >= 10)
carry = 1;
else carry = 0;
}
sum[max] = carry;
for (int j= max ; j >= 0; j--)
{
if(sum[0] == 0)
{
}
*(result++) = sum[j] + '0';
}
*result = 0;
}