I am trying to sort a list of indexes based on a list of string, and I receive bellow error - Segmentation fault. I cannot understand why I receive this error and how to solve it?
#include <iostream>
#include <string.h>
using namespace std;
int main() {
int size = 5;
char* mass[size];
int num[size];
for(int i = 0; i < size; i++) {
mass[i] = new char[20];
num[i] = i;
cin >> mass[i];
}
for(int i = 0; i < size; i++){
for(int j = size; j > i; j--)
if(strcmp(mass[num[j-1]], mass[num[j]]) > 0){
int tmp = num[j-1];
num[j-1] = num[j];
num[j] = tmp;
}
}
for(int i = 0; i < size; i++){
cout << mass[num[i]] << ", ";
}
return 0;
}
In the inner loop you start with j = size and then num[j] is an out-of-bounds array access.
In modern C++ you would solve this like this:
#include <iostream>
#include <array>
#include <algorithm>
int main() {
const int size = 5;
std::array<std::string, size> mass;
std::array<int, size> num;
for (int i = 0; i < size; i++) {
std::cin >> mass[i];
num[i] = i;
}
std::ranges::sort(num, [mass](int a, int b) { return mass[a] <= mass[b];});
for(int i = 0; i < size; i++){
std::cout << mass[num[i]] << ", ";
}
std::cout << std::endl;
return 0;
}
Related
I want to tranpose a matrix using vector?
//I am trying to 'tranpose' a vector matrix, but it's not running
#include <iostream>
#include <vector>
using namespace std;
// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}}}
// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m/2; j++) {
int temp = arr[i][j];
arr[i][j] = arr[i][j-1-i];
arr[i][j-1-i] = temp;
}}}
vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
int main() {
transpose(arrh);
swap_vector(arrh);
for (int i=0; i<arrh.size(); i++) {
for (int j=0; j<arrh[0].size(); j++) {
cout << arrh[i][j] << " ";}
cout << std::endl;}
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
// this will tranpose matrix
int transpose(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = i; j < m; j++) {
int temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
}
// this will swap the matrix
int swap_vector(vector<vector<int>> &arr){
int n = arr.size();
int m = arr[0].size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m/2; j++) {
int temp = arr[i][j];
arr[i][j] = arr[i][m-1-j];
arr[i][m-1-j] = temp;
}
}
}
vector<vector<int>> arrh{{1,2,3,4},{5,6,7,8},
{9,10,11,12},
{13,14,15,16} };
int main() {
transpose(arrh);
swap_vector(arrh);
for (int i=0; i<arrh.size(); i++) {
for (int j=0; j<arrh[0].size(); j++) {
cout << arrh[i][j] << " ";
}
cout << std::endl;
}
return 0;
}
I wanna sort an array from largest to smallest number and make a new array which has it sorted...
so here is my code:
#include <iostream>
using namespace std;
int main()
{
int size, sum = 0, answer = 0,pos, max;
int array[size];
int array2[size];
cin >> size;
for (int i = 0; i < size; i++)
{
cin >> array[i];
sum+=array[i];
}
for (int i = 0; i < size; i++)
{
max = 0;
pos = 0;
for (int q = 0; q < size; q++)
{
if (array[q] > max)
{
max = array[q];
pos = q;
}
}
array2[i] = max;
array[pos] = 0;
}
for (int i = 0; i < size; i++)
{
cout << array2[i] << ", ";
}
return 0;
}
When I put my input:
5
1 2 3 4 5
The output I get is:
0, 0, 0, 0, 5,
but I expect it to be 5, 4, 3, 2, 1,
First of all always initialize a variable when you create it as by default it has some garbage value in C++,
Also you are trying to assign a size variable (as size for an array) that has nothing assign to it yet which will create problems, Secondly you are initializing an array first and then you are taking the size variable from user which is completely opposite of the flow, for creating arrays with dynamic size see How Dynamic Array works and is implemented in C++
Updated Code:
#include <iostream>
using namespace std;
int main()
{
int size=0, sum = 0, answer = 0,pos, max;
cin >> size;
int array[size];
int array2[size];
for (int i = 0; i < size; i++)
{
cin >> array[i];
sum+=array[i];
}
for (int i = 0; i < size; i++)
{
max = 0;
pos = 0;
for (int q = 0; q < size; q++)
{
if (array[q] > max)
{
max = array[q];
pos = q;
}
}
array2[i] = max;
array[pos] = 0;
}
for (int i = 0; i < size; i++)
{
cout << array2[i] << ", ";
}
return 0;
}
Here is the Output
Edit:
As Per #PaulMcKenzie method, the other way which is considered the appropriate one, uses the std::Vector method to initialize a dynamic array in C++, people who use the first method in visual studio might face errors,
Second Method Updated Code:
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int size=0, sum = 0, answer = 0,pos, max;
cin >> size;
std::vector<int> array(size), array2(size);
for (int i = 0; i < size; i++)
{
cin >> array[i];
sum+=array[i];
}
for (int i = 0; i < size; i++)
{
max = 0;
pos = 0;
for (int q = 0; q < size; q++)
{
if (array[q] > max)
{
max = array[q];
pos = q;
}
}
array2[i] = max;
array[pos] = 0;
}
for (int i = 0; i < size; i++)
{
cout << array2[i] << ", ";
}
return 0;
}
Second Output
The size of an array variable must be a compile-time constant. A user-supplied value at runtime is probably unknowable at compile time.so I recommend using std::vector instead of array.
#include <iostream>
#include<vector>
#include <algorithm>
int main()
{
int size=0, input=0;
std::cout << "enter size :";
std::cin >> size;
std::vector<int> vec;
for (size_t i{ 0 }; i < size; ++i)
{
std::cout << "enter "<<i<< ".input:";
std::cin >> input;
vec.push_back(input);
}
// Sort the elements of the vector in descending order
for (const auto& i : vec)
std::sort(vec.begin(), vec.end(), std::greater <>());
//Print the elements of the vector
for (const auto& i : vec)
std::cout << i << " ,";
return 0;
}
Output:
enter size :5
enter 0.input:1
enter 1.input:2
enter 2.input:3
enter 3.input:4
enter 4.input:5
5 ,4 ,3 ,2 ,1 ,
I would like to point out some random integers using the regular print function, then print again the same integers using pointer notation. When I use pointer notation I run into some trouble. If anyone could send some tips it'd be much appreciated. If i comment out a specific line of code, the program will compile completely, but not with the outputs I'd like.
#include <iostream>
#include <ctime>
#include <stdlib.h> //srand(), rand()
using namespace std;
void printArray(int[], int);
//void printToday(int , );
int main()
{
int i = 0;
const int SZ = 100;
int myArray[SZ] ={0};
srand(time(0));
int myArrayTotal = 0;
int *thelight;
thelight = myArray;
for (int i = 0; i <=100; i++)
{
myArray[i]= i+rand()%1000 ;
}
cout << "Array Notation:\n\n";
printArray(myArray, SZ);
system("pause");
system("cls");
cout << "Pointer Notation: \n\n";
int k = 0;
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
cout<< *(thelight + k)<< "\t";
++k; //if I comment out this line the second part of the program will run, but it isn' the values I want.
} cout<< endl;
}
}
void printArray(int ArrayName[], int ArraySize)
{
int k = 0;
for (int i = 0; i < 10; ++i)
{
for(int j = 0; j < 10 ; ++j)
{
cout << ArrayName[k] << "\t";
++k;
}cout << endl;
}
}
Thank you
I have it so that the user defines the size of the vector and then a vector is filled. Then that vector is sorted using bubble sort (homework assignment). However, when the "sorted" vector is displayed, there are different numbers in it than what were in the original creation. How do I first create the vector, display it, then sort it and display it??
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
size = data.size();
//Sorting
void bubbleSort(vector<int> & data);
{
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
First off:
make sure to include all necessary header files, e.g. stdlib.h for your used rand() function.
get rid of all unused variables, like average, median and size.
declare your bubbleSort function outside of main function, and add additional checkup code to prevent sort if list has not more than one element.
The sort problem is related to this code snippet of yours:
for (int i = 0; i<size -1 - k; i++) { ... }
Simply remove -1
To fix your sort problem, and for better output, use following code:
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <cmath>
using namespace std;
void bubbleSort(vector<int>& data)
{
if(data.size() <= 1)
return;
for(int i=1; i<data.size(); i++)
{
for(int j=0; j<data.size()-i; j++)
{
if(data.at(j) > data.at(j+1))
{
int temp = data.at(j+1);
data.at(j+1) = data.at(j);
data.at(j) = temp;
}
}
}
}
int main()
{
int n;
vector<int> data;
cout << "Vector Length?: ";
cin >> n;
// Filling vector
for(int i=0; i<n; i++)
data.push_back(rand()%10+1);
cout << "Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
// Sorting
bubbleSort(data);
cout << endl << "Sorted Vector: ";
for(int i=0; i<data.size(); i++)
cout << data.at(i) << ", ";
return 0;
}
Hope this helps ;)
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
void printVector(vector<int> & data)
{
for (int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
//Sorting
void bubbleSort(vector<int> & data);
{
size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 - k; i++)
{
if (data[i] > data[i +1])
{
int temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
}
}
int main()
{
int n;
double average=0.0;
int median = 0;
double size = 0.0;
int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
printVector(data);
bubbleSort(data);
printVector(data);
}
If you are planning to define function void bubbleSort(vector & data) later you need to declare it before calling it.\
void bubbleSort(vector<int> & data);
int main()
{
// Here your code
bubbleSort(data);
//Here your code
}
You need to define variables only just before you need it. And if you declare and never use it, you will get unused variable warnings. So better you can comment all these variables. You can un-comment whenever you need.
//double average=0.0;
//int median = 0;
You should call your function bubbleSort() from main() as follows:
bubbleSort(data);
You are not using the iterator indexes properly to sort the elements of vector. If you change your function bubbleSort as follows it will work
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
complete program for your reference:
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
using namespace std;
//Sorting
void bubbleSort(vector<int> & data)
{
int size = data.size();
for (int k = 1; k < size; k++)
{
for (int i = 0; i<size -1 ; i++)
{
if (data[i] > data[k])
{
int temp = data[i];
data[i] = data[k];
data[k] = temp;
}
//cout<<"Sorted vector: "<< data[i]<<endl;
}
}
}
int main()
{
int n;
//double average=0.0;
//int median = 0;
//double size = 0.0;
//int i=0;
cout<<"Vector Length?: "<<endl;
cin>>n;
// int n =10;
vector<int> data;
srand(time(NULL));
//Filling vector
for (int i=0; i<n; i++)
{
data.push_back(rand()%10+1);
}
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
bubbleSort(data);
std::cout<<"sorted vector"<<"\n";
for (unsigned int i=0; i<data.size(); i++)
{
cout<<"Vector: "<< data[i]<<endl;
}
}
I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}