Sorting an array— issue - c++

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

Related

How to create array by user input?

I have a C++ problem:
Input an sequence of digit [ 0 - 9 ] and terminated by three 9 consecutivly, print on standard output the number of subsequences consisting of three consecutive equal digits on standard output.
Example: Given the sequence { 1 2 2 2 2 0 0 3 3 3 7 9 9 9 }, the subsequence are identified:
{ 2 2 2}, { 2 2 2 }, { 3 3 3 } .
Therefore, the program should print on standard output the number 3, equal to sequences present.
I try to use an array. My code ended up like this:
int main(){
int i;
int N = 0, A[100];
while( (A[i] && A[i+1] && A[i+2]) != 9 ){
N++;
for( i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:";
cin >> A[i];
}
for(int i = 0; i <= N; i++ ){
cout << "A[" << i + 1 << "]:" << A[i];
}
}
}
My problem is, I have no idea how to terminate the sequence by three 9's consecutively. So I try to use an array. I hope someone can help me to elaborate the idea.
You can do that by breaking the loop when three consecutive 9 is found.
#include <iostream>
const int ARRAY_SIZE = 100;
int main(){
int i;
int N = ARRAY_SIZE, A[ARRAY_SIZE];
for( i = 0; i < ARRAY_SIZE; i++ ){
std::cout << "A[" << i + 1 << "]:";
std::cin >> A[i];
// stop when three consecutive 9 is found
if (i >= 2 && A[i - 2] == 9 && A[i - 1] == 9 && A[i] == 9){
N = i + 1;
break;
}
}
for(int i = 0; i < N; i++ ){
std::cout << "A[" << i + 1 << "]:" << A[i] << '\n';
}
return 0;
}
Instead of an array with the number of elements equal to the magic number 100 what you need is an array of exactly three elements.
Here is a demonstrative program.
#include <iostream>
int main()
{
const size_t N = 3;
int a[N];
size_t count = 0;
for ( size_t i = 0, j = 0; std::cin >> a[j++]; )
{
j %= N;
if ( i != N - 1 )
{
++i;
}
else
{
size_t k = 1;
while ( k < N && a[k] == a[k-1] ) k++;
if ( k == N )
{
if ( a[0] == 9 ) break;
else ++count;
}
}
}
std::cout << "count = " << count << '\n';
return 0;
}
If to enter the sequence of numbers
1 2 2 2 2 0 0 3 3 3 7 9 9 9
then the program output will be
count = 3
instead of the inner while loop you could use for example the algorithm std::all_of.

How to write this table in a for loop?

Desired output:
2
4 2
6 4 2
8 6 4 2
Here is my code:
for(int i = 0; i <= 7; i += 2) {
for (int j = 0; j <= i; j += 2)
cout << j + 2 << " ";
cout << endl;
}
and I get a wrong answer.
Your approach with the double for loop was a good idea. Here is my generalized attempt, which allow to print what you want in n lines, rather than just 4:
#include <iostream>
using namespace std;
int main() {
int n = 4;
for(int i = 0; i < n; ++i) {
for(int j = i; j >= 0; --j) {
cout << 2 * (j + 1) << " ";
}
cout << "\n";
}
return 0;
}
Output:
2
4 2
6 4 2
8 6 4 2
Live Demo
PS: This approach minimizes (if not exterminates) the usage of magic numbers.
Welcome to Stack overflow.
You just went the wong way around with your inner for loop
#include <iostream>
using namespace std;
int main()
{
for(int i = 0; i <= 7; i += 2){
for (int j = i; j >= 0; j -= 2) {
cout << j + 2 << " ";
}
cout << endl;
}
return 0;
}
this gives you the desired output:
2
4 2
6 4 2
8 6 4 2

How to print the index of the first element of a sequence of the longest repeating number?

I am writing a program for an assignment, which is:
Write a program that creates an int array of 20 elements, would ask the user how many tosses are required(1-20), fills the array with the result of tossing a number cube as many times as requested by the user, prints the values of the tosses horizontally then displays the longest run found
in the sequence of tosses.
For the above example the output would be if the user requested 18 tosses:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 5 5 4 3 1 2 2 2 2 6 1 3 3 5 5 5 5
The longest run occurs at index 14.
I wrote the program for the first part, but I am stuck on trying to find the index of the longest run. This is my code so far:
const int SIZE = 20;
int main()
int arr[SIZE];
int numToss;
srand(time(0));
cout << "How many tosses are required? Enter a value from 1 to 20: " << endl;
cin >> numToss;
while (numToss > SIZE || numToss < 1)
{
cout << "Please enter a value for the number of tosses greater than 1 and less than 20: " << endl;
cin >> numToss;
}
for (int i = 0; i < numToss; i++)
{
arr[i] = 1 + rand() % 6;
cout << " " << arr[i];
}
cout << endl;
int max_count = 0;
int length = arr[0];
for (int i = 0; i < length; i++)
{
int count = 1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count > max_count)
max_count = count;
}
int result = 0;
for (int i = 0; i < length; i++)
{
int count=1;
for (int j = i + 1; j < length; j++)
{
if (arr[i] == arr[j])
count++;
}
if (count == max_count)
{
max_count = arr[i];
result = i;
cout << "The longest run occurs at " << result << endl;
}
}
return 0;
It does not print correctly, how ca i fix this? Any help is appreciated!

How do you make a rectangle and triangle shape in C++

How do i achieve this format when i keyed in int of ' 245 ':
(where if it is odd number it will be a rectangle, and even number will be triangle)
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
This is my code so far:
(I can't seemed to output triangle and rectangle at the same time)
int n;
int lastDigit;
do
{
cout << "Enter a positive integer: ";
cin >> n;
}while ( n <= 1 || n == '0');
cout << endl;
// If even digit - tri
do
{
lastDigit = n%10;
if (lastDigit / 2 ==0)
{
for (int i = 1; i <= lastDigit; ++i)
for (int tri = 1; tri <= i; ++tri)
cout << "\t" << tri;
cout << endl;
}
// if odd digit - rect
else if (lastDigit / 2 != 0)
{
for (int i = 1; i <= lastDigit; i++)
{
for (int rect = 1; rect <= i; rect++)
cout << "\t" << rect;
cout << endl;
}
n = n/10;
}
cout << endl;
}while (lastDigit != 0);
n = n/10;
cout << endl;
return 0;
And, how should i code when keyed in the int, the compiler will extract the first digit (From left to right) and output it accordingly?
Any help would be appreciated!
Following is the complete code.
Step1: Take user input and check whether it's odd or even.
Step2: If it is odd then perform triangle else rectangle.
#include<iostream>
int main () {
int n;
cout<<"Enter number: ";
cin>>n;
if (n % 2 != 0)
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= n; j++)
{
cout<<j<<" ";
}
}
}
else
{
for(int i = 1; i <= n; i++)
{
cout<<endl;
for(int j = 1; j <= i; j++)
{
cout<<j<<" ";
}
}
}
return 0;
}
Both of your print is similar. I suggest:
#include<iostream>
using namespace std;
int main () {
int n;
cout << "Enter number: ";
cin >> n;
int i = 1;
int& rowLim = ((n % 2) ? n : i);
for(i = 1; i <= n; i++)
{
cout << endl;
for(int j = 1; j <= rowLim; j++)
{
cout<<j<<" ";
}
}
return 0;
}
Simplest would be to use a string, iterate char by char and output accordingly. For instance
#include <iostream>
using namespace std;
int main() {
string s;
cin >> s;
for (char c : s) {
int n = c - '0';
bool k = n % 2;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= (k ? n : i); ++j)
cout << " " << j;
cout << endl;
}
cout << endl;
}
return 0;
}
Output
1
1 2
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
See a DEMO

Floyd pattern in c++

hi i am try to print the following pattern in c++
1
2 3
4 5 6
7 8 9 10
7 8 9 10
4 5 6
2 3
1
using the following loop a print half of it.
int i,j,k=1;
cout<<"Enter row";
cin>>n;
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<k<<"\t";
k++;
}
}
i got the output like
1
2 3
4 5 6
7 8 9 10
how can i print the balance output. but how can i print the mirror of this pattern.
First of all, the code above is wrong, it should be:
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
cout<<k<< " ";
k++;
}
cout << '\n';
}
Now, to draw the pattern, you could use a loop that would use the following logic:
for(;i > 0; i--)
{
k -= i-1;
for(j=1;j<i;j++)
{
cout <<k<< " ";
k++;
}
cout << '\n';
k -= i-1;
}
int main()
{
int n;
int i, j, k = 1;
cout << "Enter row";
cin >> n;
int elemcount = 0;
for (i = 1; i <= n; i++)
{
elemcount = 0;
for (j = 1; j <= i; j++)
{
cout << k << "\t";
k++;
}
cout <<endl;
}
k = k - n; //Reset Counter to the value of the first digit in current row.
i--;
j--;
for (; i > 0; i--)
{
j = i;
elemcount = 0; //Counter to keep track of elements printed.
for (; j> 0; j--)
{
cout << k << "\t";
k++;
elemcount++;
}
k = k - elemcount - (i-1); //Resetting K, substracting the number of elements printed and number of elements to be printed in next row.
cout << endl;
}
return 0;
}