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.
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;
}
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)
I did the same question a while ago, but It got closed. I'll try to express myself better this time.
I want to make an algorithm that can solve a sudoku puzzle, looks like It is working, but in the backtracking part(where I need to go back to a previous recursion to test a different value), It doesn't, showing a "segment fault" error.
Also, I use "0" as a blank space, so that's why "if (!board[i][j]) //do something"
// Solves the game
vector<vector<int>> sudokuSolver(vector<vector<int>> board) {
if (isFull(board)) {
return board;
}
bool found = false;
int line = 0;
int col = 0;
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
if (!board[i][j]) {
line = i;
col = j;
found = true;
break;
}
}
if (found) break;
}
vector<int> possibleNumbers = possibilities(board, line, col);
cout << '\n';
printBoard(board);
cout << '\n';
cout << line << ' ' << col << '\n';
cout << '\n';
printVector(possibleNumbers);
int size = possibleNumbers.size();
for (int k = 0; k < size; k++) {
board[line][col] = possibleNumbers[k];
cout << k << '\n';
sudokuSolver(board);
// the code doesn't pass to the next cout. Why?
cout << "it doesn't reach here :/" << '\n';
}
cout << "backtracking!!" << '\n';
board[line][col] = 0;
cout << "agora:" << '\n';
printBoard(board);
}
I put some "couts" to help me visualize the problem. The "backtracking!!" cout is working properly, but the "it doesn't reach here" cout is not. I thought that, after the code realize It doesn't have possible solutions, It would simply go back to the last recursion, but It's not, giving "segmento fault" error. I don't understand
for (int k = 0; k < size; k++) {
board[line][col] = possibleNumbers[k];
cout << k << '\n';
sudokuSolver(board);
// the code doesn't pass to the next cout. Why?
cout << "it doesn't reach here :/" << '\n';
}
Am I not getting something?
Also, if It's not clear, "isFull" checks if the board is full (base case, game is complete), and possibilities checks the number possibilities of a single cell
Thanks in advance. I guess the details are way better this time, hope I did It right
Edit: The extra couts are ways of showing me where the code is. They are not important for the algorithm itself
Final edit: I did it! Thanks you all. For anyone who wants the full code, here it is:
#include <bits/stdc++.h>
using namespace std;
bool gameRunning = true;
void printBoard(vector<vector<int>> board) {
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
cout << board[i][j] << ' ';
}
cout << '\n';
}
cout << '\n';
}
void printVector(vector<int> v) {
for (auto x : v) {
cout << x << ' ';
}
cout << '\n';
cout << '\n';
}
// Checks if the board is full, deciding if the game is already won
bool isFull(vector<vector<int>> board) {
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
if (!board[i][j]) return false;
}
}
return true;
}
// Generates all number possibilites for a given cell on the board
vector<int> possibilities(vector<vector<int>> board, int i, int j) {
bitset<9> p;
for (int i = 1; i <= 9; i++) {
p[i] = 1;
}
//horizontal check
for (int col = 0; col <= 8; col++) {
if (board[i][col]) {
p[board[i][col]] = 0;
}
}
//vertical check
for (int line = 0; line <= 8; line++) {
if (board[line][j]) {
p[board[line][j]] = 0;
}
}
//mini-square check
int linesquare = (i / 3) * 3;
int colsquare = (j / 3) * 3;
for (int l = linesquare; l <= linesquare + 2; l++) {
for (int c = colsquare; c <= colsquare + 2; c++) {
if (board[l][c]) {
p[board[l][c]] = 0;
}
}
}
vector<int> numberPossibilities;
for (int k = 1; k <= 9; k++) {
if (p[k]) numberPossibilities.push_back(k);
}
return numberPossibilities;
}
// Solves the game
void sudokuSolver(vector<vector<int>> board) {
if (isFull(board)) {
printBoard(board);
gameRunning = false;
}
if (gameRunning) {
bool found = false;
int line = 0;
int col = 0;
for (int i = 0; i <= 8; i++) {
for (int j = 0; j <= 8; j++) {
if (!board[i][j]) {
line = i;
col = j;
found = true;
break;
}
}
if (found) break;
}
vector<int> possibleNumbers = possibilities(board, line, col);
int size = possibleNumbers.size();
for (int k = 0; k < size; k++) {
board[line][col] = possibleNumbers[k];
sudokuSolver(board);
}
board[line][col] = 0;
}
}
int main() {
/*
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
*/
vector<vector<int>> v;
for (int i = 0; i < 9; i++) {
vector<int> hold;
for (int j = 0; j < 9; j++) {
int a;
cin >> a;
hold.push_back(a);
}
v.push_back(hold);
}
cout << '\n';
sudokuSolver(v);
return 0;
}
Bye, have a great day
I know this might be a duplicate to another question on this forum but I couldn't find the solution for my problem, even if I searched for like 1 hour.
The problem is that my program stops after the 4th "cin". I don't know why, I tried everything: "cin.ingore(); cin.clear();", "cin.get();".
Could someone help me please?
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
struct elev
{
char nume[20];
vector<int> note_info;
float medie;
};
int main()
{
int n, e = 0;
vector<elev> elevi;
cout << "n = "; cin >> n;
for (int i = 1; i <= n; i++)
{
int s = 0, nr;
elevi.push_back(elev());
cout << "Nume elev: "; cin >> elevi[i].nume;
cout << "Numar note informatica: "; cin >> nr;
for (int j = 0; j < nr; j++)
{
int temp;
cout << "Nota nr. " << j + 1 << ": "; cin >> temp;
elevi[i].note_info.push_back(temp);
s += temp;
}
elevi[i].medie = (float)(s / nr);
}
for (int i = 1; i <= n; i++)
{
for (int j = i; j <= n; j++)
{
if (elevi[j].medie != elevi[j + 1].medie)
{
e += 1;
}
}
}
if (e)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].medie < elevi[j + 1].medie)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
else
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - i; j++)
{
if (elevi[j].nume > elevi[j + 1].nume)
{
elev temp = elevi[j];
elevi[j] = elevi[j + 1];
elevi[j + 1] = temp;
}
}
}
}
cout << "Rezultate:";
for (int i = 1; i <= n; i++)
{
cout << '\n' << elevi[i].nume << ' ' << setprecision(2) << fixed << elevi[i].medie;
}
return 0;
}
Replace this line:
for (int i = 1; i <= n; i++)
with
for (int i = 0; i < n; ++i)
The error stems from trying to access the vector elevi at a position it doesn't yet have. Because vectors start indexing at 0, the first access made to elevi should be at index 0.
So I'm trying to read a matrix A from a text file, which it does correctly. A vector B is entered by the user. Then I want to perform Gaussian Elimination (Ax = b) to get the solution vector x. The values I get for x are -1.#IND and I have no idea why...I'm guessing something is going wrong in SystemSolution?
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
//this program does gaussian elimination for a matrix Ax=b
vector<double> SystemSolution(vector<vector<double>> A, vector<double> b)
{
//Determine and test size of a matrix
int n = A.size();
for (int i = 0; i < n; i++)
if (n != A[i].size())
throw "Error! Number of rows and columns of matrix must be equal!";
vector<double> x(b.size());
//x is the vector of solutions
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
//Finding pivot
double pivot = A[i][i];
int index = i;
for (int k = i + 1; k < n; k++)
{
if (pivot > abs(A[k][i]))
{
index = k;
pivot = A[k][i];
}
}
//Row exchange
for (int k = 0; k < n; k++)
{
double tmp = A[i][k];
A[i][k] = A[index][k];
A[index][k] = tmp;
}
//Elimination
double coefficient = -(A[j][i] / A[i][i]);
for (int k = i; k < n; k++)
{
A[j][k] += coefficient*A[i][k];
}
b[j] += coefficient*b[i];
}
}
//Back-substitution
x[n - 1] = b[n - 1] / A[n - 1][n - 1];
for (int i = n - 2; i >= 0; i--)
{
double sum = 0;
for (int j = i; j < n; j++)
{
sum += x[j] * A[i][j];
}
x[i] = (b[i] - sum) / A[i][i];
}
return x;
}
void PrintVector(const vector<double> &b)
{
for (int i = 0; i < b.size(); i++)
cout << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setprecision(4)
<< setw(8) << b[i] << endl;
}
void PrintMatrix(const vector<vector<double> > &A)
{
for (int i = 0; i < A.size(); i++)
{
for (int j = 0; j < A[i].size(); j++)
cout << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setprecision(4)
<< setw(8) << A[i][j];
cout << endl;
}
}
int main()
{
int n;
cout << "Please enter the number of rows/columns:";
cin >> n;
ifstream matrixFile;
matrixFile.open("matrix.txt");
if (matrixFile.is_open()){
//matrix A values
vector<vector<double>> A(n, vector<double>(n));
vector<double> b(n);
string line;
int col = 0;
int row = 0;
while (getline(matrixFile, line)){
istringstream stream(line);
int x;
col = 0; //reset
while (stream >> x){
A[row][col] = x;
col++;
}
row++;
}
cout << "Please enter vector b:"<<endl;
//vector b values
for (int i = 0; i < row; i++)
{
cout << "b[" << i + 1 << "]= ";
cin >> b[i];
}
vector<double> x = SystemSolution(A, b);
cout << "- SOLUTIONS -" << endl;
cout << "Matrix:" << endl;
PrintMatrix(A);
cout << "\nVector x:" << endl;
PrintVector(x);
}
else{
cout << "File failed to open!";
}
matrixFile.close();
return 0;
}
There could be some divisions by zero in your code:
double coefficient = -(A[j][i] / A[i][i]);
/* .. */
x[n - 1] = b[n - 1] / A[n - 1][n - 1];
/* .. */
x[i] = (b[i] - sum) / A[i][i];
Check out Gauss-elimination here:
Square Matrix Inversion in C
Review and debug yours.