How is my for-loop messing up? - c++

I am having a problem with my for loops in this code that I've written. Everything runs fine except it does not catch the value for the 4th row in my string array ("Jill", "Jason", "Jim".)
I'm pretty sure the problem is happening in my function definition for 'Initialize' since the values for the final ID and final list of team members is not being caught by my vector. For reference, the output is fine up until the final TeamV element. I've tried messing with the indices a ton but I've hit a brick wall so-to-speak. Thanks in advance.
#include <vector>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
const int NUM_TEAMS = 4;
const int NUM_MEMBERS = 3;
struct TeamS {
int id;
string m[NUM_MEMBERS];
}TeamV, tempTeam;
void Initialize(vector <TeamS> & TeamV, const int id[],
const string m[][NUM_MEMBERS], int arraySize);
void printList(const vector <TeamS> & TeamV);
int main()
{
const int ID[NUM_TEAMS] = { 123, 321, 456, 789 };
const string MEMBERS[NUM_TEAMS][NUM_MEMBERS] =
{
{ "Sarah", "Joe", "John" },
{ "Chris", "Kevin", "James" },
{ "Tom", "Kim", "Emily" },
{ "Jill", "Jason", "Jim" }
};
vector<TeamS> TeamV;
Initialize(TeamV, ID, MEMBERS, NUM_TEAMS);
cout << TeamV[3].m[1];
printList(TeamV);
// not working on my installation at home -> system("pause");
cin.get();
}
void Initialize(vector<TeamS>& TeamV, const int id[], const string m[][NUM_MEMBERS], int arraySize)
{
//Resizes a TeamS vector and then modifies it's values to match the information provided
TeamV.resize(arraySize+1);
//iterate through teams
for (int i = 0; i < arraySize+1; i++)
{
TeamV[i].id = id[i];
//iterate through members of teams
for (int j = 0; j < NUM_MEMBERS; j++)
{
TeamV[i].m[j] = m[i][j];
}
}
}
void printList(const vector<TeamS>& TeamV)
{
cout << "**** Team List ****\n\n";
int teamVSize = TeamV.size();
for (int i = 0; i < teamVSize; i++)
{
cout << "Information for team " + to_string(i) << endl;
cout << "ID:\t" << TeamV[i].id << "\t";
for (int j = 0; j < NUM_MEMBERS; j++)
{
cout << TeamV[i].m[j] << "\t";
}
cout << endl;
}
}

Your for loop goes out of bounds of the array. Get rid of +1
for (int i = 0; i < arraySize; i++)

You pass in a 2D array with 4 rows, yet in your initialise() function you loop 5 times:
TeamV.resize(arraySize + 1); // Why plus 1?
//iterate through teams
for (int i = 0; i < arraySize + 1; i++) // Why plus 1?
{
TeamV[i].id = id[i];
//iterate through members of teams
for (int j = 0; j < NUM_MEMBERS; j++)
{
TeamV[i].m[j] = m[i][j]; // Because you loop four times on the
// last loop i = 4, which is outside the string
// array you passed, which has only 4 rows.
}
}
Zero index is the first, index i = 4 means the 5th row, you overrun the array you created, I get an std::bad_alloc exception.
If you leave out the + 1 from "arraySize + 1" it works fine.
By the way, the way it is now, it fills in correctly the 4 rows up until it throws the bad_alloc exception, so I don't understand why you said it leaves out the 4th row.

Related

Replacing the value of map elements isnt working

I'm just Learning about std::map and its functions . I recently got this problem. I tried making a program which prints out a grid type data where I used std::map for the keys and values . The program prints out fine but I wanted to make a program where once I erased a data in that grid, other data above that should move down one step and the topmost would have 0 in it . somehow I tried but it doesn't seem to work . I don't know where I did wrong in that . My code:
in class header:
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include<map>
#define x_Pair std::pair<unsigned int,unsigned int>
class MapCheck
{
public:
std::map<x_Pair, unsigned int>m_MapData;
void SetMapData();
x_Pair GetBlockCell(int num);
void EraseCell(int cell);
};
in class cpp:
void MapCheck::SetMapData()
{
int count = 1;
for (int j = 0; j < 20; j++)
{
for (int i = 0; i < 10; i++)
{
m_MapData[{i, j}] = count;
count++;
}
}
}
x_Pair MapCheck::GetBlockCell(int num)
{
for (int j = 0; j < 20; j++)
{
for (int i = 0; i < 10; i++)
{
if (m_MapData[{i, j}] == num)
{
return x_Pair(i, j);
}
}
}
return x_Pair(-1, -1);
}
void MapCheck::EraseCell(int cell)
{
x_Pair pair = GetBlockCell(cell);
for (int i = pair.second; i < 20; i++)
{
m_MapData[{pair.first, i}] = m_MapData[{pair.first, i - 1}];
m_MapData[{pair.first, i - 1}] = 0;
}
}
-and in main:
#include"MapCheck.h"
int main()
{
MapCheck mc;
mc.SetMapData();
std::string input;
do
{
system("cls");
for (int j = 0; j < 20; j++)
{
for (int i = 0; i < 10; i++)
{
std::cout << mc.m_MapData[{i, j}] << " ";
}
std::cout << std::endl;
}
std::cout << "Enter a number to erase or E to exit";
std::cin >> input;
mc.EraseCell(std::atoi(input.c_str()));
} while (input != "E");
return 0;
}
the output without any inputs :
after entering number 191 in the input :
expected result:
All Except the printing is fine . I dont Get Where I did Wrong. Any help would be appreciated. Thanks in advance!!
The order of
for (int i = pair.second; i < 20; i++)
{
m_MapData[{pair.first, i}] = m_MapData[{pair.first, i - 1}];
m_MapData[{pair.first, i - 1}] = 0;
}
is element found to the bottom. When you want to move everything above the item removed down one slot, this isn't all that useful. So lets flip it around.
for (int i = pair.second; i > 0; i--)
{
m_MapData[{pair.first, i}] = m_MapData[{pair.first, i - 1}];
}
m_MapData[{pair.first, 0}] = 0;
This starts at the item being removed and goes up to slot 1, copying each item down one slot. To handle the top of the column, we have m_MapData[{pair.first, 0}] = 0; to set the top item to zero, something we only need to do once.
Side note: Unless we have a sparse array, this would be a lot more efficient with a 2D array in place of the map.
You have following:
x_Pair MapCheck::GetBlockCell(int num)
which is used as
x_Pair pair = GetBlockCell(cell);
This will invoke copy constructor of std::pair<>.
I think you need to return and use reference:
x_Pair& MapCheck::GetBlockCell(int num)
which is used as
x_Pair& pair = GetBlockCell(cell);

Can I print the whole array in cpp instead of address [duplicate]

I am trying to print a text file out on screen using arrays, but I'm not sure why it does not appear the way it is in the text file.
The text file:
1 2 3 4
5 6 7 8
Displayed on the screen as follows after applying discard function:
1
2
3
4
5
6
7
8
The code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
const int MAX_SIZE = 20;
const int TOTAL_AID = 4;
void discard_line(ifstream &in);
void print(int print[][4] , int size);
int main()
{
//string evnt_id[MAX_SIZE]; //stores event id
int athlete_id[MAX_SIZE][TOTAL_AID]; //stores columns for athelete id
int total_records;
char c;
ifstream reg;
reg.open("C:\\result.txt");
discard_line(reg);
total_records = 0;
while( !reg.eof() )
{
for (int i = 0; i < TOTAL_AID; i++)
{
reg >> athlete_id[total_records][i] ;//read aid coloumns
}
total_records++;
reg.get(c);
}
reg.close();
print(athlete_id, total_records);
system("pause");
return 0;
}
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}
void print(int print[][4] , int size)
{
cout << " \tID \t AID " << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < TOTAL_AID; j++)
{
cout << print[i][j] << endl;
}
}
}
You are printing std::endl after each number. If you want to have 1 row per line, then you should print std::endl after each row. Example:
#include <iostream>
int main(void)
{
int myArray[][4] = { {1,2,3,4}, {5,6,7,8} };
int width = 4, height = 2;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
std::cout << myArray[i][j] << ' ';
}
std::cout << std::endl;
}
}
Also note that writing using namespace std; at the beginning of your files is considered bad practice since it causes some of user-defined names (of types, functions, etc.) to become ambiguous. If you want to avoid exhausting prefixing with std::, use using namespace std; within small scopes so that other functions and other files are not affected.
It is not only mistake that you miss the "endl".
The program will also skip the first line in the source file because of calling the function discard_line(reg), so you only can get the others data(5 6 7 8). It is not necessary to use the function at all.
in addition, make sure that you init the array and check boundary of array, such as MAX_SIZE, to guarantee the input data not to overflow the array.
you can do it like this
#include <iostream>
int your_array[2][4] = {
{1,2,3,4},
{5,6,7,8}
};
using namespace std;
int main() {
// get array columns and rows
int rows = sizeof your_array / sizeof your_array[0];
int cols = sizeof your_array[0] / sizeof(int);
// Print 2d Array
cout << "your_array data "<<endl<<endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cout << your_array[i][j] << std::endl;
}
// std::cout << std::endl;
}
}
output
1
2
3
4
5
6
7
8

Filling an 1D array in C++

I have an integer array:
int listint[10] = {1,2,2,2,4,4,5,5,7,7,};
What I want to do is to create another array in terms of the multiplicity. So I define another array by:
int multi[7]={0};
the first index of the multi array multi[0] will tell us the number of multiplicity of the array listint that has zero. We can easily see that, there is no zero in the array listint, therefore the first member would be 0. Second would be 1 spice there are only 1 member in the array. Similarly multi[2] position is the multiplicity of 2 in the listint, which would be 3, since there are three 2 in the listint.
I want to use an for loop to do this thing.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
unsigned int count;
int j;
int listint[10] = { 1,2,2,2,4,4,5,5,7,7, };
int multi[7] = { 0 };
for (int i = 0; i < 9; i++)
{
if (i == listint[i])
count++;
j = count;
multi[j] = 1;
}
cout << "multi hit \n" << multi[1] << endl;
return 0;
}
After running this code, I thought that I would want the multiplicity of the each element of the array of listint. So i tried to work with 2D array.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
unsigned int count;
int i, j;
int listint[10] = { 1,2,2,2,4,4,5,5,7,7, };
int multi[7][10] = { 0 };
for (int i = 0; i < 9; i++)
{
if (i == listint[i])
count++;
j = count;
for (j = 0; j < count; j++) {
multi[j][i] = 1;
}
}
cout << "multi hit \n" << multi[4][i] << endl;
return 0;
}
The first code block is something that I wanted to print out the multiplicity. But later I found that, I want in a array that multiplicity of each elements. SO isn't the 2D array would be good idea?
I was not successful running the code using 2D array.
Another question. When I assign j = count, I mean that that's the multiplicity. so if the value of count is 2; I would think that is a multiplicity of two of any element in the array listint.
A 2d array is unnecessary if you're just trying to get the count of each element in a list.
#include <iostream>
int main() {
int listint[10] = { 1,2,2,2,4,4,5,5,7,7, };
int multi[8] = { 0 };
for (int i : listint)
++multi[i];
for (int i = 0; i < 8; ++i)
std::cout << i << ": " << multi[i] << '\n';
return 0;
}
There's also a simpler and better way of doing so using the standard collection std::map. Notably, this doesn't require you to know what the largest element in the array is beforehand:
#include <map>
#include <iostream>
int main() {
int listint[10] = {1,2,2,2,4,4,5,5,7,7,};
std::map<int, int> multi;
for (int i : listint)
multi[i]++;
for (auto [k,v] : multi)
std::cout << k << ": " << v << '\n';
}
Try this incase maps won't work for you since you're a beginner, simple:
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
unsigned int count;
int j;
int listint[10] = {1,2,2,2,4,4,5,5,7,7};
int multi[8]={0};
for(int i=0; i<10; i++)
{
multi[listint[i]]++; // using listint arrays elements as index of multi to increase count.
}
for( int i=1; i<8; i++)
{
cout << "multi hit of "<<i<<" : "<< multi[i]<<endl;
}
return 0;
}
OR if numbers could get large and are unknown but sorted
#include <iostream>:
#include <stdio.h>
using namespace std;
int main()
{
unsigned int count = 0;
int index = 0; // used to fill elements in below arrays
int Numbers[10] = {0}; // storing unique numbers like 1,2,4,5,7...
int Count[10] = {0}; // storing their counts like 1,3,2,2,2...
int listint[10] = {1, 2, 2, 2, 4, 4, 5, 5, 7, 7};
for(int i = 0; i < sizeof(listint) / sizeof(listint[0]); i++)
{
count++;
if (listint[i] != listint[i+1]) {
Numbers[index] = listint[i];
Count[index] = count;
count=0;
index++;
}
}
for(int i=0; i<index; i++)
{
cout << "multi hit of "<<Numbers[i]<<" is " << Count[i]<<endl;
}
return 0;
}

Calculating the inverse of a 3x3 Matrix, cannot use previous matrix to calculate inverse [duplicate]

I am trying to print a text file out on screen using arrays, but I'm not sure why it does not appear the way it is in the text file.
The text file:
1 2 3 4
5 6 7 8
Displayed on the screen as follows after applying discard function:
1
2
3
4
5
6
7
8
The code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
using namespace std;
const int MAX_SIZE = 20;
const int TOTAL_AID = 4;
void discard_line(ifstream &in);
void print(int print[][4] , int size);
int main()
{
//string evnt_id[MAX_SIZE]; //stores event id
int athlete_id[MAX_SIZE][TOTAL_AID]; //stores columns for athelete id
int total_records;
char c;
ifstream reg;
reg.open("C:\\result.txt");
discard_line(reg);
total_records = 0;
while( !reg.eof() )
{
for (int i = 0; i < TOTAL_AID; i++)
{
reg >> athlete_id[total_records][i] ;//read aid coloumns
}
total_records++;
reg.get(c);
}
reg.close();
print(athlete_id, total_records);
system("pause");
return 0;
}
void discard_line(ifstream &in)
{
char c;
do
in.get(c);
while (c!='\n');
}
void print(int print[][4] , int size)
{
cout << " \tID \t AID " << endl;
for (int i = 0; i < size; i++)
{
for (int j = 0; j < TOTAL_AID; j++)
{
cout << print[i][j] << endl;
}
}
}
You are printing std::endl after each number. If you want to have 1 row per line, then you should print std::endl after each row. Example:
#include <iostream>
int main(void)
{
int myArray[][4] = { {1,2,3,4}, {5,6,7,8} };
int width = 4, height = 2;
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
std::cout << myArray[i][j] << ' ';
}
std::cout << std::endl;
}
}
Also note that writing using namespace std; at the beginning of your files is considered bad practice since it causes some of user-defined names (of types, functions, etc.) to become ambiguous. If you want to avoid exhausting prefixing with std::, use using namespace std; within small scopes so that other functions and other files are not affected.
It is not only mistake that you miss the "endl".
The program will also skip the first line in the source file because of calling the function discard_line(reg), so you only can get the others data(5 6 7 8). It is not necessary to use the function at all.
in addition, make sure that you init the array and check boundary of array, such as MAX_SIZE, to guarantee the input data not to overflow the array.
you can do it like this
#include <iostream>
int your_array[2][4] = {
{1,2,3,4},
{5,6,7,8}
};
using namespace std;
int main() {
// get array columns and rows
int rows = sizeof your_array / sizeof your_array[0];
int cols = sizeof your_array[0] / sizeof(int);
// Print 2d Array
cout << "your_array data "<<endl<<endl;
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < cols; ++j)
{
std::cout << your_array[i][j] << std::endl;
}
// std::cout << std::endl;
}
}
output
1
2
3
4
5
6
7
8

Error implementing selection sort in C++

I've written this code to sort an array using selection sort, but it doesn't sort the array correctly.
#include <cstdlib>
#include <iostream>
using namespace std;
void selectionsort(int *b, int size)
{
int i, k, menor, posmenor;
for (i = 0; i < size - 1; i++)
{
posmenor = i;
menor = b[i];
for (k = i + 1; k < size; k++)
{
if (b[k] < menor)
{
menor = b[k];
posmenor = k;
}
}
b[posmenor] = b[i];
b[i] = menor;
}
}
int main()
{
typedef int myarray[size];
myarray b;
for (int i = 1; i <= size; i++)
{
cout << "Ingrese numero " << i << ": ";
cin >> b[i];
}
selectionsort(b, size);
for (int l = 1; l <= size; l++)
{
cout << b[l] << endl;
}
system("Pause");
return 0;
}
I can't find the error. I'm new to C++.
Thanks for help.
The selectionSort() function is fine. Array init and output is not. See below.
int main()
{
int size = 10; // for example
typedef int myarray[size];
myarray b;
for (int i=0;i<size;i++)
//------------^^--^
{
cout<<"Ingrese numero "<<i<<": ";
cin>>b[i];
}
selectionsort(b,size);
for (int i=0;i<size;i++)
//------------^^--^
{
cout<<b[l]<<endl;
}
system("Pause");
return 0;
}
In C and C++, an array with n elements starts with the 0 index, and ends with the n-1 index. For your example, the starting index is 0 and ending index is 9. When you iterate like you do in your posted code, you check if the index variable is less than (or not equal to) the size of the array, i.e. size. Thus, on the last step of your iteration, you access b[size], accessing the location in memory next to the last element in the array, which is not guaranteed to contain anything meaningful (being uninitialized), hence the random numbers in your output.
You provided some sample input in the comments to your question.
I compiled and executed the following, which I believe accurately reproduces your shown code, and your sample input:
#include <iostream>
void selectionsort(int* b, int size)
{
int i, k, menor, posmenor;
for(i=0;i<size-1;i++)
{
posmenor=i;
menor=b[i];
for(k=i+1;k<size;k++)
{
if(b[k]<menor)
{
menor=b[k];
posmenor=k;
}
}
b[posmenor]=b[i];
b[i]=menor;
}
}
int main(int argc, char **argv)
{
int a[10] = {-3, 100, 200, 2, 3, 4, -4, -5, 6, 0};
selectionsort(a, 10);
for (auto v:a)
{
std::cout << v << ' ';
}
std::cout << std::endl;
}
The resulting output was as follows:
-5 -4 -3 0 2 3 4 6 100 200
These results look correct. I see nothing wrong with your code, and by using the sample input you posted, this confirms that.