I'm trying to show the simulation of this bubble sort and I used a function swapper that has reference values but when I try to print it after the swap it also prints out the memory address. How can I fix this?
void swapper(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return;
}
int main(){
//Bubble sort
int arr[] = {-2, 45, 0, 11, -9};
int n = 5;
for(int step = 0; step < n-1; step++) {
for(int i = 0; i < n; i++) {
if(arr[i] > arr[i + 1])
swapper(arr[i], arr[i + 1]);
}
for(int x = 0; x < n; x++)
cout << arr[x] << " ";
cout << endl;
}
Fixing the bugs
Try this:
#include <iostream>
using namespace std;
void swapper(int &a, int &b) {
int temp = a;
a = b;
b = temp;
return;
}
int main(){
//Bubble sort
int arr[] = {-2, 45, 0, 11, -9};
int n = 5;
for(int step = 0; step < n-1; step++) {
for(int i = 0; i+1 < n; i++) {
if(arr[i] > arr[i + 1])
swapper(arr[i], arr[i + 1]);
}
for(int x = 0; x < n; x++)
cout << arr[x] << " ";
cout << endl;
}
}
I added a missing } and changed the condition of the inner loop to i+1 < n to avoid array access out of bounds.
This is the output:
-2 0 11 -9 45
-2 0 -9 11 45
-2 -9 0 11 45
-9 -2 0 11 45
Aligning the output
You can make the output aligned more nicely by replacing
cout << arr[x] << " ";
with
cout << setw(2) << arr[x] << " ";
and writing
#include <iomanip>
at the top of your file. Then the output is
-2 0 11 -9 45
-2 0 -9 11 45
-2 -9 0 11 45
-9 -2 0 11 45
Related
hi so i need to fill an 2D array of dummy values like x with values form txt file, the "x" are there to fill in empty cols if file.txt has little or no data my file.txt looks like this
1 23 3 42 5 63 4 . 5
-2 1 43 95 55 5 43 2 -6
. 2 3 -4 5 43 -4 4 35
82 61 3 5 -5 65 . 2 6
my c++ looks like this
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
int main(){
ifstream file;
ofstream newfile;
string filename;
string text;
const int n = 10;
char x = 'x';
double A[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
A[i][j] = x;
cout << A[i][j];
}
}
do{
cout << "Podaj nazwe pliku(wraz z rozszerzeniem): ";
cin >> filename;
file.open(filename.c_str());
}while (!file.is_open());
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if (!file.eof()){
getline(file, text);
A[i][j] = stod(text, 0);
}else{
break;
}
}
}
file.close();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
cout << A[i][j];
}
cout << endl;
}
}
when i print out this array i get output like 127.77098e-3184 i can't copy my input because it's not printing right now i don't know why
Problem:
When you use std::getline you take the whole line instead of taking one number.
"." is not a valid argument for std::stod so it will throw an std::invalid_argument exception.
Solution:
Use the std::ifstream::operator>> instead.
Reformat the file and change the "." to the value it should represent (I guess it is 0). Even semantically a point by itself does not represent a number.
Additional information:
using namespace std; is considered a bad practice.
The newFile variable is unused.
The x variable should be a double.
The <cmath> header is unused.
You may use std::array.
Full code:
file.txt
1 23 3 42 5 63 4 0 5
-2 1 43 95 55 5 43 2 -6
0 2 3 -4 5 43 -4 4 35
82 61 3 5 -5 65 0 2 6
main.cpp
#include <iostream>
#include <fstream>
int main(){
std::string filename;
std::string text;
constexpr int n = 10;
double x = -1;
double A[n][n];
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
A[i][j] = x;
std::cout << A[i][j] << " ";
}
std::cout << std::endl;
}
std::ifstream file;
do{
std::cout << "Podaj nazwe pliku(wraz z rozszerzeniem): "; //Insert filename.
std::cin >> filename;
std::cout << std::endl;
file.open(filename);
}while (!file.is_open());
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
if(file >> text){
A[i][j] = std::stod(text, 0);
}
else{
break;
}
}
}
file.close();
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
std::cout << A[i][j] << " ";
}
std::cout << std::endl;
}
}
I am having trouble with making a turn inside a two dimensional array to output the elements in spiral. I tried this code, but it is outputting not enough elements, I tried to make some if statements outside of the loop to cover all cases for which the general algorithm doesn't output. Can you help suggesting some way to manage the correct output.
CODE
#include <iostream>
#include <algorithm>
//#include <cmath>
using namespace std;
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c) / 2; runs--;) {
for (int i = c_beg; i < c_end; i++)
cout << m[r_beg][i] << " ";
for (int i = r_beg; i < r_end; i++)
cout << m[i][c_end] << " ";
for (int i = c_end; i > c_beg; i--)
cout << m[r_end][i] << " ";
for (int i = r_end; i > r_beg; i--)
cout << m[i][c_beg] << " ";
r_beg++;
c_beg++;
r_end--;
c_end--;
}
if (r <= c && c_beg <= c_end) {
for (int i = c_beg; i <= c_end; i++)
cout << m[r_end][i] << " ";
}
else if (r >= c && r_beg <= r_end) {
for (int i = r_beg; i <= r_end; i++)
cout << m[i][c_end] << " ";
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Example:
Input:
3 3
1 2 3
4 5 6
7 8 9
Output: 1 2 3 6 9 8 7 4 5
If you have for example 3x10 matrix. It doesn't output.
Input:
3
7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
Output: should get to 13, but it stops at 8.
The code, as presented, loops towards center until the smaller of the two dimensions is consumed up. However, if that smaller dimension has odd size, then parts of the central row or column respectively haven't been printed out. You can cover that one with some special case handling after your outer loop:
for (int runs = std::min(r, c) / 2; runs--;)
{
// ...
}
if(c < r)
{
if(c & 1)
{
for (int i = r_beg; i <= r_end; i++)
// ^ (!)
// don't forget to print last element: there's no second loop
// that would print the corner element a second time now!
std::cout << m[i][c_end] << " ";
}
}
else
{
// handles the square matrix case as well
if(r & 1)
{
for (int i = c_beg; i <= c_end; i++)
std::cout << m[r_beg][i] << " ";
}
}
This can be solved by carefully fine-tuning the bail-out conditions / ranges of the for loops:
#include <iostream>
using namespace std; // :-(
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int r, c;
cin >> r >> c;
int r_beg = 0, r_end = r - 1, c_beg = 0, c_end = c - 1;
int **m = new int*[r];
for (int i = 0; i < r; i++)
{
m[i] = new int[c];
for (int j = 0; j < c; j++)
{
cin >> m[i][j];
}
}
for (int runs = min(r, c);;)
{
for (int i = c_beg; i <= c_end; i++)
cout << " " << m[r_beg][i];
++r_beg;
for (int i = r_beg; i <= r_end; i++)
cout << " " << m[i][c_end];
--c_end;
if (!--runs) break;
for (int i = c_end; i >= c_beg; i--)
cout << " " << m[r_end][i];
--r_end;
for (int i = r_end; i >= r_beg; i--)
cout << " " << m[i][c_beg];
++c_beg;
if (!--runs) break;
}
for (int i = 0; i < r; i++)
delete[] m[i];
delete[] m;
return 0;
}
Input:
3 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Output:
1 2 3 4 5 6 7 14 21 20 19 18 17 16 15 8 9 10 11 12 13
Live Demo on ideone
Input:
4 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Notes:
I changed the bail-out of the for loop:
Instead of min(r, c) / 2, I use min(r, c) and decrement/check runs twice in the body.
I adjusted the update of r_beg, r_end, c_beg, and c_end.
I'm asking for help with a problem implying an array sorting in the following manner: all even numbers must be in front of the odd ones. I've partially made the problem, but I did the sorting in the opposite manner and I can not manage to fix it.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int v[100], n, i, aux = 0, inv;
cout << "Number of elements: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]=";
cin >> v[i];
}
do
{
inv = 0;
for (i = 0; i < n; i++)
{
if (v[i] % 2 == 1 && v[i + 1] % 2 == 0)
{
inv = 1;
aux = v[i];
v[i] = v[i + 1];
v[i + 1] = aux;
}
}
} while (inv != 0);
cout << endl;
for (i = 0; i < n; i++)
cout << v[i] << " ";
cout << endl;
system("pause");
return 0;
}
The output for this would be:
n = 8
1 3 2 4 7 8 4 2
Result: 2 4 8 4 2 -858993460 1 3
In the expression v[i + 1], you access v[n] when i = n - 1, this will result in an out-of-bounds memory access which results in undefined behaviour.
You should change the for loop to this:
for (i = 0; i < n - 1; i++)
The output for the given input is:
a.exe
Number of elements: 8
v[0]=1
v[1]=3
v[2]=2
v[3]=4
v[4]=7
v[5]=8
v[6]=4
v[7]=2
2 4 8 4 2 1 3 7
I have following program. with Input 3 5
3 rows
5 growth of numbers
The output should be:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
But my program gives:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
Here is what I have tried so far
int main() {
int n, m, c = 0;
cin >> n >> m;
int a[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i][j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << setw(4) << a[i][j];
cout << endl;
}
}
What I am doing wrong or missing?
About the spaces: Can't find reason for such behavior(first spaces are ignored), displayed on screenshot. Tried to run in different IDE's with different compilers and had such problem only in testing system.
Hi try to use tab instead.
#include <iostream>
using namespace std;
int main() {
int n, m, c = 0;
cin >> n >> m;
int *a = new int[n * m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
a[i * n + j] = ++c;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)
cout << "\t" << a[i * n + j];
cout << endl;
}
delete[] a;
return 0;
}
Can't remember how I solved this problem in secondary school, but with n less than m, the following code works:
#include <iostream>
using namespace std;
void nextij(long n,long m,long& i,long& j) {
if (i==n-1) { //bottom row
if (j<n-1) { //in the left square
j = i+j+1;
i = 0;
}
else { //out of the left square
i = j-(n-1)+1;
j = m-1;
}
}
else { //other rows
if (j==0) { //left most column
j = i+1;
i = 0;
}
else { //other columns
i++;
j--;
}
}
}
int main() {
long n = 3;
long m = 5;
long a[3][5];
long i = 0;
long j = 0;
long c = 1;
while (c<=n*m) {
a[i][j] = c;
nextij(n,m,i,j);
c++;
}
for (i=0; i<n; i++) {
for (j=0; j<m; j++)
cout <<a[i][j] <<" ";
cout <<endl;
}
}
/*
output:
1 2 4 7 10
3 5 8 11 13
6 9 12 14 15
*/
I have to figure out a way to add two binary numbers that are stored in arrays, and the binary numbers can be of any size; i.e.:
Sample input and output:
101 + 11 = 1000
111 + 111 = 1110
1010 + 1010 = 10100
11101 + 1010 = 100111
11111 + 11111 = 111110
This is way over my head but this is what I have so far:
#include <iostream>
using namespace std;
void addition (int a[], int b[], int sizea, int sizeb, int result[]){
int carry = 0;
for(int i = 0; i < sizeof(a); i++)
{
if(a[i] + b[i] + carry == 0)
{
result[i] = 0;
carry = 0;
}
else if(a[i]+b[i]+carry==1)
{
result[i]=1;
carry=0;
}
else if(a[i] + b[i] + carry == 2)
{
result[i] = 0;
carry = 1;
}
else if(a[i] + b[i] + carry > 2)
{
result[i] = 1;
carry = 1;
}
}
result[0] = 0;
for (int i = 10; i > 0; i--){
cout << result[i];
}
cout << endl;
}
void initarray(int &sizea, int &sizeb, int a[], int b[]){
cout << "Enter size of a" << endl;
cin >> sizea;
cout << "Enter size of b" << endl;
cin >> sizeb;
cout << "enter contents of a " << sizea << endl;
for (int i = 0; i < sizea; i++){
cin >> a[i];
}
cout << "enter contents of b "<< sizeb << endl;
for (int z = 0; z < sizeb; z++){
cin >> b[z];
}
}
int main() {
int sizea, sizeb;
int* a = new int[sizea];
int* b = new int[sizeb];
int* result = new int [10];
initarray(sizea, sizeb, a, b);
addition(a, b, sizea, sizeb, result);
}
Please feel free to rip me apart, I'm really having trouble with this and I think I have the logic down, I just can't figure out how to translate it into code.
Right now, if I enter in the first example, I get:
Enter size of a
3
Enter size of b
2
enter contents of a 3
1 0 1
enter contents of b 2
1 1
-18174002763276720728465360000100
So obviously there's a problem here. Can someone help?
Adapt your code like this:
void addition (int a[], int b[], int sizea, int sizeb, int result[]){
int maxSize = sizea > sizeb ? sizea : sizeb; // number of bits is maximum of siza and sizeb
int carry = 0;
for( int i = 0; i < maxSize; i++ )
{
int bitA = i < sizea && a[i] ? 1 : 0; // test if bit in array a is set
int bitB = i < sizeb && b[i] ? 1 : 0; // test if bit in array b is set
int sum = bitA + bitB + carry; // calculate sum of all bits
result[i] = sum == 1 || sum == 3 ? 1 : 0; // result bit is set if sum is equal 1 or 3
carry = sum > 1 ? 1 : 0; // carry bit is set if sum is eaul 2 or 3
}
result[ maxSize ] = carry; // highest bit of result is carry bit
for (int i = 0; i <= maxSize; i++){
cout << result[maxSize-i];
}
cout << endl;
}