How come no matter what I do for rows and columns my columns won't go above two? It should be 8x8 adding the 8 numbers together 8 times. I don't know what I'm doing wrong. Thank you
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main() {
srand(time(0));
int array1[8][8];
int array2[8][8];
int addition[8][8];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++)
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 7; i++) {
for (int j = 0; j < 7; j++) {
array2[i][j] = rand() % 8;
}
}
{
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
addition[i][j] = array1[i][j] + array2[i][j];
}
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
cout << array1[i][j];
cout << " " << array2[i][j];
cout << " " << endl;
cout << "both previous numbers added together = " << addition[i][j] << endl;
}
}
return 0;
}
}
}
Hi look carefully at the code structure.
you had some extra brackets.
This is the correct structure:
#include <iostream>
//using namespace std; - use this only in simple projects (my opinion).
int main()
{
srand(time(0));
int array1[8][8];
int array2 [8][8];
int addition[8][8];
for (int i = 0;i < 8;i++)
{
for (int j = 0; j< 8;j++)
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
array2[i][j] = rand() % 8;
}
//{ - you dont need this here
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
addition[i][j] = array1[i][j] + array2[i][j];
}
for (int i = 0;i < 8;i++)
{
for (int j = 0;j < 8;j++)
{
std::cout << array1[i][j];
std::cout << " " << array2[i][j];
std::cout << " " << std::endl;
std::cout << "both previous numbers added together = " << addition[i][j] << std::endl;
}
}
//} - and you don't need this here
return 0;
}
Take this example and compare to your code to see your mistake. Code just wasn't structured properly.
Your code's logics are absolutely right! However, the mistake was found in improper bracket structuring on for loop. I have corrected your code and mentioned the mistakes as comments.
#include <iomanip>
#include <cstdlib>
#include <iostream> //include this header to use "cout"
using namespace std;
int main() {
srand(time(0));
int array1[8][8];
int array2[8][8];
int addition[8][8];
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++)
array1[i][j] = rand() % 6;
}
for (int i = 0;i < 7; i++) {
for (int j = 0; j < 7; j++) {
array2[i][j] = rand() % 8;
}
}
{
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
addition[i][j] = array1[i][j] + array2[i][j];
}
} //add this bracket
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 7; j++) {
cout << array1[i][j];
cout << " " << array2[i][j];
cout << " " << endl;
cout << "both previous numbers added together = " << addition[i][j] << endl;
//} remove this bracket
}
return 0;
}
}
}
Also, to add on, if you want an 8x8 matrix use i<8 and j<8 everywhere in the code. Here you have used i<7 and j<7 which means you get a 7x7 matrix as result.
Logic:
i=0 to i<7 use have => 0,1,2,3,4,5,6 (stopping at 6 because 6<7 is true and 7<7 becomes false naturally). So from 0 to 6 there are totally 7 elements.
Hope this helps! :)
Related
When I try to start my program it keeps saying "undefined reference to 'pthread_create' and 'pthread_join' . The program is supposed to multiply two matrix A and B and the result should be put in matrix C, the first thread should calculate first 2 rows of matrix C, and the 2nd thread is supposed to calculate the 2nd two rows of matrix C. The program also works fine without the threads. I'm new to threads so I can't figure out where I messed up, hope someone could help me out?
This is my code:
#include <cstdlib>
#include <ctime>
#include <csignal>
#include <pthread.h>
using namespace std;
int MAX_m, MAX_n, MAX_l;
int *matA = new int[MAX_m * MAX_l];
int *matB = new int[MAX_m * MAX_l];
int *matC = new int[MAX_l * MAX_l];
int rd_i = 0;
void* umnoz(void* arg)
{
int i = rd_i++;
for (int j = 0; j < MAX_l; j++)
{
for (int k = 0; k < MAX_m; k++)
{
matC[i * MAX_l + j] += matA[i * MAX_m + k] * matB[k * MAX_l + j];
}
}
}
pthread_t *dretve;
int main(int argc, char *argv[]) {
MAX_l = atoi(argv[1]);
MAX_m = atoi(argv[2]);
MAX_n = atoi(argv[3]);
for (int i = 0; i < MAX_l; i++)
{
for (int j = 0; j < MAX_m; j++)
{
matA[i * MAX_m + j] = rand() % 10;
}
}
for (int i = 0; i < MAX_m; i++)
{
for (int j = 0; j < MAX_l; j++)
{
matB[i * MAX_l + j] = rand() % 10;
}
}
cout << endl;
cout << "Mnozenik: " << endl;
for (int i = 0; i < MAX_l; i++)
{
for (int j = 0; j < MAX_m; j++)
{
cout << matA[i * MAX_m + j] << " ";
cout << endl;
}
}
cout << endl;
cout << "Mnozitelj: " << endl;
for (int i = 0; i < MAX_m; i++)
{
for (int j = 0; j < MAX_l; j++)
{
cout << matB[i * MAX_l + j] << " ";
cout << endl;
}
}
dretve = new pthread_t [MAX_n];
for (int i = 0; i < MAX_n; i++)
{
int *p;
pthread_create(&dretve[i], NULL, umnoz, (void *) (p));
}
for (int i = 0; i < MAX_n; i++)
{
pthread_join(dretve[i], NULL);
}
cout << endl;
cout << "Umnozak: " << endl;
for (int i = 0; i < MAX_l; i++)
{
for (int j = 0; j < MAX_l; j++)
{
cout << matC[i * MAX_l + j] << " ";
cout << endl;
}
}
return 0;
}
a scheduling problem that wants to minimize a weighted completion time
I would like to pass the image problem to the c++ language and solve it with cplex solver.
Problem:
Min ∑WiCi
xi ∈ 0,1
t= ∆ + ∑_{i} xi*pi1
Ci1 = ∑_{j=1}^{i} xj*pj1
Ci2 = t + ∑_{j=1}^{i} (1−xj)*pj2
Ci ≥ Ci1
Ci ≥ Ci2−Ωxi
Ω = ∆ + ∑_i max(pi1, pi2)
The goal is to minimize the weighted completion time of a problem with one machine with at most one machine reconfiguration. I have a boolean variable Xi that tells me if the job is done before or after the reconfiguration. The variable t is the instant after the reconfiguration(delta is the time to reconfigure the machine). pi1 is the processing time in configuration 1 and pi2 is the processing time in configuration 2. Besides, I have restrictions due to Ci (completion time of a job i).
The example of code I made is not working. I know I have a problem writing the C variable (but I don't know how to solve it).
#include <iostream>
#include <ilcplex/ilocplex.h>
#include <ilcp/cp.h>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int nbJob = 4;
int delta = 2;
int nbConf = 2;
vector<int> w_job;
w_job.push_back(1); w_job.push_back(1); w_job.push_back(1); w_job.push_back(1);
vector<vector<int>>p_job;
p_job.resize(nbJob);
p_job[0].push_back(6); p_job[0].push_back(3);
p_job[1].push_back(1); p_job[1].push_back(2);
p_job[2].push_back(10); p_job[2].push_back(2);
p_job[3].push_back(1); p_job[3].push_back(9);
int max_p = 0;
int aux;
for (size_t i = 0; i < nbJob - 1; i++) {
aux = max(p_job[i][0], p_job[i][1]);
max_p = max(max_p, aux);
}
cout << max_p << endl;
int max_w = 0;
aux = 0;
for (size_t i = 0; i < nbJob - 1; i++) {
aux = max(w_job[i], w_job[i + 1]);
max_w = max(aux, max_w);
}
cout << max_w << endl;
int omega = 0;
for (size_t i = 0; i < nbJob; i++) {
omega += max(p_job[i][0], p_job[i][1]);
}
omega += delta;
cout << omega << endl;
try {
IloEnv env;
IloModel model(env);
IloBoolVarArray x(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloBoolVar xi(env, 0, 1, "xi");
x[i] = xi;
}
IloArray<IloExprArray> C(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloExprArray Ci(env);
for (size_t j = 0; j < nbConf; j++) {
IloExpr Cij(env);
Ci.add(Cij);
}
C[i] = Ci;
}
IloNumVarArray C_final(env, nbJob);
for (size_t i = 0; i < nbJob; i++) {
IloNumVar C_finali(env, 0, IloInfinity, ILOINT); //
C_final[i] = C_finali;
}
IloExpr t(env);
for (int i = 0; i < nbJob; i++) {
t += x[i] * p_job[i][0];
}
t += delta;
for (size_t i = 0; i < nbJob; i++) {
for (size_t j = 0; j < nbJob; j++) {
if (j <= i) {
C[i][0] += x[j] * p_job[j][0];
}
}
}
for (size_t i = 0; i < nbJob; i++) {
for (size_t j = 0; j < nbJob; j++) {
if (j <= i) {
C[i][1] += ((1 - x[j]) * p_job[j][1]);
}
}
C[i][1] += t;
}
for (size_t i = 0; i < nbJob; i++) {
model.add(C_final[i] >= C[i][0]);
}
for (size_t i = 0; i < nbJob; i++) {
model.add(C_final[i] >= C[i][1] - (omega * x[i]));
}
IloExpr wiCi(env);
for (size_t i = 0; i < nbJob; i++) {
wiCi += w_job[i] * C_final[i];
}
model.add(IloMinimize(env, wiCi));
IloCplex solver(model);
solver.solve();
for (int i = 0; i < nbJob; i++) {
cout << "C " << i + 1 << " = " << solver.getValue(C_final[i]) << " x" << i+1 << " = " << solver.getValue(x[i]) << endl;
}
cout << endl;
cout << "t = " << solver.getValue(t) << endl;
cout << "wiCi = " << solver.getObjValue() << endl << endl;
}
catch (IloException e) {
cout << e.getMessage();
}
}
What's wrong with my code
I Have to add all the edge values of the 2D Array.
The Function is not returning what I want. Its returning garbage value.
Program Req Output : Add all the edges value of the 2D Array and return its answer.
You are not allowed to use Vector etc.
Here is my Code :
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
int k = 0;
int l = 0;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= cols; j++)
{
if (i == 1 || i == rows || j == 1 || j == cols)
{
sum = arr[k][l];
totalOfEdges = sum + totalOfEdges;
}
l++;
}
k++;
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
My Output :
Leaving the Correct Code in Case Any one need it .
#include <iostream>
using namespace std;
const int rows = 3;
const int cols = 4;
void AddEdges (int arr[rows][cols])
{
int sum = 0;
int totalOfEdges = 0;
int i,j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (i == 0 || i == (rows-1) || j == 0 || j == (cols-1))
{
sum = arr[i][j];
totalOfEdges = sum + totalOfEdges;
}
}
}
cout << "Total of Edges = " << totalOfEdges << endl;
}
int main()
{
int arr[3][4] = {0};
cout << "Enter Elements for Array = \n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cin >> arr[i][j];
}
}
cout << "--------" << endl;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
AddEdges(arr);
}
So, I'm trying to swap the matrix elements with respect to the main diagonal. I have tried using temp method (switching values while using temp variable), also tried std::swap(a,b). Somehow it only swaps upper right side of the matrix and leaves another half not changed.
How do I make everything swap?
My code:
#include <iostream>
using namespace std;
int main()
{
const int n = 7;
srand (time(NULL));
int matrix[n][n];
cout << "Original Matrix :" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
(i == j) ? matrix[i][j] = 0 : matrix[i][j] = rand() % 100+1;
cout << matrix[i][j] << "\t";
}
cout << endl;
}
cout << "\nRemade Matrix:" << endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
// swap(matrix[i][j], matrix[j][i]); //another method
cout << matrix[i][j] << "\t";
}
cout << endl;
}
return 0;
}
You are basically swapping them twice, replace your swapping loops with this. Notice the condition of the second loop, it's j < i. Then, you can print it with another set of loops.
for (int i = 0; i < n; i++)
for (int j = 0; j < i ; j++)
swap(matrix[i][j], matrix[j][i]);
Your logic is almost ok. Just the inner loop counter will start from "i+1" else after the swapping the value is again getting overlapped. Try the following code and make sure you understand it. Happy coding!
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << matrix[i][j] << "\t";
}
cout << endl;
}
I am currently working on a program that prints a 5 variable truth table. I am using a 2d array. My code currently produces the table, but says it is corrupt and "the stack around the variable "table" was corrupted. Any help?
#include <iostream>
using namespace std;
int main() {
bool table[5][32];
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
This is homework, so I would like to understand it, not just have an answer.
The index is wrong. Only table[0] to table[4] are available, so accessing table[5] to table[31] is illegal.
Try this:
#include <iostream>
using namespace std;
int main() {
bool table[32][5]; // swap 32 and 5
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
table[i][j] = ((i >> j)& 1);
}
}
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 5; j++) {
cout << table[i][j] << " ";
}
cout << endl;
}
return 0;
}
There is attempt to read out of bound values from array.
If you need 5x32 matrix Use code below:
for (int i = 0; i < 5; i++) { // 32-> 5
for (int j = 0; j < 32; j++) { // 5->32
If you need 32x5 matrix then replace code below:
bool table[32][5]; //it was table[5][32];