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;
}
Related
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! :)
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();
}
}
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)
It says my vector is out of range for ArrayD. But I do not understand why. Everything else works. Is says that the vector is out of range after double deez. Therefore, it will not ascend fully.
using namespace std;
void hello();
void Infile();
double x, y, z;
double P, B;
int E = 0;
double V[16];
double C[16];
double D[16];
int m, n;
int main()
{
Infile();
return 0;
}
void Infile()
{
ifstream fileA("P_matrix.txt");
ifstream fileB("b_matrix.txt");
ifstream fileC("d_matrix.txt");
vector<double>ArrayP;
vector<double>ArrayB;
vector<double>ArrayD;
cout << "P Matrix Values \n";
while (fileA.good())
{
fileA >> P;
ArrayP.push_back(P);
}
for (int i = 0; i<ArrayP.size(); i++)
{
cout << ArrayP[i] << ",";
}
system("pause");
cout << "B Matrix Values \n";
while (fileB.good())
{
fileB >> B;
ArrayB.push_back(B);
}
for (int j = 0; j < 16; j++)
{
cout << ArrayB[j] << ",";
}
system("pause");
while (fileC.good())
{
fileC >> D;
ArrayD.push_back(D);
}
for (int k = 0; k <16; k++)
{
cout << ArrayD[k] << ",";
}
system("pause");
for (int m = 0; m < 16; m++)
{
V[m] = ArrayP[m] * ArrayB[m];
cout << V[m] << ",";
}
system("pause");
for (int n = 0; n < 16; n++)
{
C[n] = V[n] * ArrayD[n];
cout << C[n] << ",";
}
//outfile.close();
system("pause");
double deez;
for (int d = 0; d < 16; d++) //acscending
{
for (int q = 0; q < 16; q++)
{
if (ArrayD[q] < ArrayD[q - 1])
{
deez = ArrayD[q];
ArrayD[q] = ArrayD[q - 1];
ArrayD[q - 1] = deez;
}
}
}
for (int q = 0; q < 16; q++)
cout << C[q] << ",";
system("pause");
double nutz;
for (int d = 0; d < 16; d++) //descending
{
for (int q = 0; q < 16; q++)
{
if (V[q] < V[q + 1])
{
nutz = V[q];
V[q] = V[q + 1];
V[q + 1] = nutz;
}
}
}
for (int q = 0; q < 16; q++)
cout << V[q] << ",";
system("pause");
return;
}
Here, q starts at zero, so you are attempting to access index [-1].
ArrayD[q - 1]
When iterating over ArrayP, you correctly do:
for (int i = 0; i<ArrayP.size(); i++)
However for ArrayB and ArrayC, you seem to assume there are only ever 16 elements in those arrays (are you sure?). I can't see any evidence that the arrays are restricted to always having 16 elements in code, so that could also be the issue.
Later still, you then seem to assume all of those arrays have 16 elements?
for (int m = 0; m < 16; m++)
{
V[m] = ArrayP[m] * ArrayB[m];
cout << V[m] << ",";
}
Those are the obvious places that could cause an out of range error.
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;
}