I want to print the following number sequence:
1 2 3 6 7 8 11 12 13 16 17 18...
It prints three positive integers then skips two following values and then repeats the process.
Here's a simple way just using a for loop statement:
for (int i = 1; i < 100; i += ((i%5) == 3) ? 3 : 1)
{
// ...
}
As pointed out by Franck you can simply use the modulo operator.
The modulo operator gives you the rest of a division as a result.
0 / 10 = 0; 0 % 10 = 0;
10 / 10 = 1; 10 % 10 = 0;
11 / 10 = 1; 11 % 10 = 1;
12 / 10 = 1; 12 % 10 = 2;
20 / 10 = 2; 20 % 10 = 0;
21 / 10 = 2; 21 % 10 = 1;
27 / 10 = 2; 21 % 10 = 7;
0 % 3 = 0;
1 % 3 = 1;
2 % 3 = 2;
3 % 3 = 0;
4 % 3 = 1;
5 % 3 = 2;
6 % 3 = 0;
7 % 3 = 1;
8 % 3 = 2;
9 % 3 = 0;
...
From your example I assume that you want to skip values ending on 4 or 9.
You have 2 possibilities to archive this:
use % 10 and check the result for being either 4 or 9
use % 5 and check the result for being either 4
The result would look something like this:
for (int i=1; i<=100; i++)
{
if(i%5 == 4) continue; //Skip
std::cout << i << " ";
}
Related
int main()
{
int newposition, shiftSteps;
int numbers[10], numberscopy[10];
cin >> shiftSteps;
for (int i = 0; i < 10; i++)
cin >> numbers[i];
for (int i = 0; i < 10; i++)
numberscopy[i] = numbers[i];
//------------------------------------
for (int i = 9; i >= 0; i--)
{
if (i - shiftSteps < 10 && i - shiftSteps >= 0)
newposition = i - shiftSteps;
else
newposition = i - shiftSteps + 10;
numbers[newposition] = numberscopy[i];
}
for (int i = 0; i < 10; i++)
cout << numbers[i] << " ";
}
I want to rotate 10 numbers to left and "shiftSteps" is number of moves to the left. but I have a problem, the code I wrote so far for some numbers it works properly like {0 1 2 3 4 5 6 7 8 9} and shiftSteps = 3 output is 3 4 5 6 7 8 9 0 1 2.
but if inputs are 0 1 2 3 4 5 6 7 8 9 and shiftSteps = 15, the output is 5 6 7 8 9 5 6 7 8 9 and 0 Disappears, True answer for shiftSteps = 15 is 5 6 7 8 9 0 1 2 3 4.
The problem is that newposition = i - shiftSteps + 10; results in a negative value for shiftSteps == 15 and i < 5. This results in an out-of-bounds access.
You need to ensure that the rotation amount is below the number of elements of the array, which can be achieved with a modulus operator.
shiftSteps = shiftSteps % 10;
for (int i = 9; i >= 0; i--)
{
newposition = i - shiftSteps;
if (newposition < 0)
newposition += 10;
numbers[newposition] = numberscopy[i];
}
This will work for non-negative values of shiftSteps. If you also need to handle negatives, you should adjust the condition in the loop accordingly.
PS: Also, note that in your code shiftSteps is left uninitialized.
PPS: You could also use std::rotate algorithm.
I'm trying to create a Multiplication Table like this one but I'm having issues with the spacing. Could someone please explain where I'm going wrong. I got the program to work without trying to create a table but every time I attempt to create tables it goes horribly wrong. Please help.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i,j;
for(i=2; i <=9; i++)
{
cout << setw(9) << i << " ";
}
for(j = 1; j<=9; j++)
{
cout << setw (9) << (i*j) << " ";
}
printf("%d x %d = %d\n", i, j, i*j);
cout << endl;
}
You need to use nested loops. You can use the '\t' special character to insert horizontal tabs but this may not work well if you decide to make a bigger table later on. and the Here is a solution for your expected output:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, j, k;
for (k = 0; k < 2; k++)
{
for (j = 1; j < 10; j++)
{
for (i = 2 + 4*k; i < 6 + 4*k; i++)
{
cout << i << " x " << j << " = " << i * j << '\t';
}
cout << endl;
}
cout << endl;
}
return 0;
}
This outputs:
2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5
2 x 2 = 4 3 x 2 = 6 4 x 2 = 8 5 x 2 = 10
2 x 3 = 6 3 x 3 = 9 4 x 3 = 12 5 x 3 = 15
2 x 4 = 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9
6 x 2 = 12 7 x 2 = 14 8 x 2 = 16 9 x 2 = 18
6 x 3 = 18 7 x 3 = 21 8 x 3 = 24 9 x 3 = 27
6 x 4 = 24 7 x 4 = 28 8 x 4 = 32 9 x 4 = 36
6 x 5 = 30 7 x 5 = 35 8 x 5 = 40 9 x 5 = 45
6 x 6 = 36 7 x 6 = 42 8 x 6 = 48 9 x 6 = 54
6 x 7 = 42 7 x 7 = 49 8 x 7 = 56 9 x 7 = 63
6 x 8 = 48 7 x 8 = 56 8 x 8 = 64 9 x 8 = 72
6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
the program should take an integer from user and print pattern like below.
if n=3, then
333
313
323
333
if n=4, then
4444
4114
4224
4334
4444
if n=5, then
55555
51115
52225
53335
54445
55555
and so on
Here is what is have tried.
#include<iostream>
using namespace std;
int main()
{
int pattern[10][10], i, j, n;
cout << "Enter dimension of square matrix: ";
cin >> n;
for (i = 0;i <= n;i++)
{
pattern[i][0] = n;
pattern[i][n - 1] = n;
}
for (j = 1;j < n - 2;j++)
{
pattern[0][j] = n;
pattern[n][j] = n;
}
for (i = 1;i < n - 1;i++)
{
for (j = 1;j < n - 2;j++)
{
pattern[i][j] = i;
}
}
for (i = 0;i <=n;i++)
{
for (j = 0;j < n;j++)
{
cout << pattern[i][j];
cout << "\t";
}
cout << "\n";
}
return 0;
}
I am getting the right pattern but in some places, there are some garbage value (or something else)
Quite simple to do in a clear, concise manner:
#include<iostream>
int main()
{
int n = 0;
std::cout << "Enter dimension of square matrix: ";
std::cin >> n;
for (int i = 0; i < n + 1; ++i) {
for (int j = 0; j < n; ++j) {
// if on the first or last column or row: print n, else: print i
std::cout << ((i == 0 || j == 0 || i == n || j == n-1) ? n : i) << '\t';
}
std::cout << '\n';
}
return 0;
}
Example output:
Enter dimension of square matrix: 5
5 5 5 5 5
5 1 1 1 5
5 2 2 2 5
5 3 3 3 5
5 4 4 4 5
5 5 5 5 5
You are on the right way! Some simple change will give the right answer .
#include<iostream>
using namespace std;
int main()
{
int pattern[10][10], i, j, n;
cout << "Enter dimension of square matrix: ";
cin >> n;
for (i = 0;i <= n;i++)
{
pattern[i][0] = n;
pattern[i][n - 1] = n;
}
for (j = 1;j <n ;j++)
{
pattern[0][j] = n;
pattern[n][j] = n;
}
for (i = 1;i < n ;i++)
{
for (j = 1;j < n -1;j++)
{
pattern[i][j] = i;
}
}
for (i = 0;i <=n;i++)
{
for (j = 0;j < n;j++)
{
cout << pattern[i][j];
cout << "\t";
}
cout << "\n";
}
return 0;
}
I think you are overcomplicating things - there is absolutely no need for the two-dimensional array and all the loops.
You could just use a basic helper function printer, that prints the version with the nested number i and execute that in a loop from 1 to the last number, for example 5.
#include <cstdio>
#include <iostream>
int main() {
unsigned user_input = 5; // change to ask for actual user input
// Helper function, i.e. for 3 it prints 53335
auto printer = [](unsigned i) {
for (unsigned n = 0; n < user_input; ++n) {
if (n == 0 || n + 1 == user_input) std::cout << user_input;
else std::cout << i;
}
std::cout << '\n';
};
printer(user_input); // print 55555
// print the rest 51115, 52225 ... up to 55555 again
for (unsigned i = 1; i <= user_input; ++i) printer(i);
}
If you think you need the printer function elsewhere too or don't want to use a lambda, you can of course just move it and give it a signature like that:
void printer(unsigned i, unsigned user_input) {
/* copy-paste the code from printer */
}
In fact there is no need to define an array to output the pattern.
All you need is two loops.
Here is a demonstration program.
#include <iostream>
int main()
{
while ( true )
{
const unsigned int MAX_VALUE = 10;
std::cout << "Enter a non-negative number less than "
<< MAX_VALUE << " (0 - exit): ";
unsigned int n;
if ( not ( std::cin >> n ) or ( n == 0 ) ) break;
if ( MAX_VALUE - 1 < n ) n = MAX_VALUE - 1;
std::cout << '\n';
for ( unsigned int i = 0; i < n + 1; i++ )
{
for ( unsigned int j = 0; j < n; j++ )
{
if ( j == 0 || j == n - 1 )
{
std::cout << n;
}
else if ( i == 0 || i == n )
{
std::cout << n;
}
else
{
std::cout << i;
}
}
std::cout << '\n';
}
std::cout << '\n';
}
}
Its output might look like
Enter a non-negative number less than 10 (0 - exit): 1
1
1
Enter a non-negative number less than 10 (0 - exit): 2
22
22
22
Enter a non-negative number less than 10 (0 - exit): 3
333
313
323
333
Enter a non-negative number less than 10 (0 - exit): 4
4444
4114
4224
4334
4444
Enter a non-negative number less than 10 (0 - exit): 5
55555
51115
52225
53335
54445
55555
Enter a non-negative number less than 10 (0 - exit): 6
666666
611116
622226
633336
644446
655556
666666
Enter a non-negative number less than 10 (0 - exit): 7
7777777
7111117
7222227
7333337
7444447
7555557
7666667
7777777
Enter a non-negative number less than 10 (0 - exit): 8
88888888
81111118
82222228
83333338
84444448
85555558
86666668
87777778
88888888
Enter a non-negative number less than 10 (0 - exit): 9
999999999
911111119
922222229
933333339
944444449
955555559
966666669
977777779
988888889
999999999
Enter a non-negative number less than 10 (0 - exit): 0
That is you can substitute all your loops that fill the array for if statements in one pair of loops that output the pattern.
The if statements can be substituted for the conditional operator the following way
for ( unsigned int i = 0; i < n + 1; i++ )
{
for ( unsigned int j = 0; j < n; j++ )
{
std::cout << ( i % n == 0 || j % ( n - 1 ) == 0 ? n : i );
}
std::cout << '\n';
}
The restriction to output the pattern only for numbers [1, 9] is artificial.
It would be more reasonable to set the range at least to [1, 255] where 255 is the maximum value that can be stored in an object of type unsigned char.
You can get the value through the expression
std::numeric_limits<unsigned char>::max()
To make an alignment for outputted columns you can use standard function std::setw that sets the width of an outputted field.
Taking all this into account the program can look the following way.
#include <iostream>
#include <iomanip>
#include <limits>
int main()
{
while ( true )
{
const unsigned int Base = 10;
const unsigned int MAX_VALUE = std::numeric_limits<unsigned char>::max();
std::cout << "Enter a non-negative number less than or equal to "
<< MAX_VALUE << " (0 - exit): ";
unsigned int n = 0;
if ( not ( std::cin >> n ) || ( n == 0 ) ) break;
if ( MAX_VALUE < n ) n = MAX_VALUE;
// Calculating the width of numbers plus one space between them.
int number_width = 1;
unsigned int tmp = n;
do { ++number_width; } while ( tmp /= Base );
std::cout << '\n';
for ( unsigned int i = 0; i < n + 1; i++ )
{
for ( unsigned int j = 0; j < n; j++ )
{
std::cout << std::setw( number_width )
<< ( i % n == 0 || j % ( n - 1 ) == 0 ? n : i );
}
std::cout << '\n';
}
std::cout << '\n';
}
}
The program output might look like
Enter a non-negative number less than 255 (0 - exit): 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 20
20 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 20
20 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 20
20 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 20
20 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 20
20 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 20
20 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 20
20 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 20
20 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 20
20 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 20
20 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 20
20 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 20
20 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 13 20
20 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 20
20 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 20
20 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 20
20 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 20
20 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 20
20 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
Enter a non-negative number less than 255 (0 - exit): 0
As for your code then for starters you should check whether the user entered a number that is greater than 9
And for example in this pair of loops
for (i = 1;i < n - 1;i++)
{
for (j = 1;j < n - 2;j++)
{
pattern[i][j] = i;
}
}
the conditions should look at least like
for (i = 1; i < n; i++)
{
for (j = 1; j < n - 1; j++ )
{
pattern[i][j] = i;
}
}
because the height of the pattern is equal to n + 1 and the width is equal to n. So if you want to exclude the last row and last column you have to use correspondingly the expressions n and n - 1 in the conditions of the loops.
I used a for loop to find antilog of the given number.
int g = 0, m, diff = 10;
for(j = 0; g <= diff; j++)
{
g = pow(2, j);
}
m = j - 2;
cout << m;
It gives the power of 2 for which g is the number just less than diff.
I tried the base change theorem of log to find the antilog of the number something like this:
m = log(diff) / log(2);
without the for loop, but in this case whenever there is a number that is an exact multiple of 2s for example 8, then it gives 2 as the answer and not 3.
And using for loop for doing so is in a program is exceeding the time limit.
Is there a shorter and reliable way to do so?
Here is a fun solution without looping:
function antilog(int input) {
int pow2 = input - 1;
pow2 |= pow2 >> 16; // turn on all bits < MSB
pow2 |= pow2 >> 8;
pow2 |= pow2 >> 4;
pow2 |= pow2 >> 2;
pow2 |= pow2 >> 1;
pow2++; // get least pow2 >= input
return // construct binary offset of pow2 bit
((pow2 & 0xffff0000) != 0) << 4
| ((pow2 & 0xff00ff00) != 0) << 3
| ((pow2 & 0xf0f0f0f0) != 0) << 2
| ((pow2 & 0xcccccccc) != 0) << 1
| ((pow2 & 0xaaaaaaaa) != 0);
}
The latter half of which was adapted from some part of the bit twiddling hacks. (Knowing the source, there is probably some other function faster than this doing what you've asked.
Solutions aside, it should be noted that what particularly is causing your solution to be slow is the repeated calls to pow, which is a relatively expensive function. Because you are doing integer arithmetic (and what's more, multiplying by 2, every computer's favorite number), it is much more efficient to write your loop as the following:
int g=1,m,diff=10;
for(j = 0; g <= diff && g <<= 1; j++) /* empty */;
m=j-2;
cout<<m;
Which is quite the hack. int g=1 initializes g to the value it takes on the first time the code executes the body of the loop you've written. The loop conditions g <= diff && g <<= 1 evaluates to g <= diff. (Notice that this is a problem if diff >= 1 << (8 * sizeof(int) - 2), the greatest power of two we can store in an int). The empty statement simply allows us to have a well-formed for statement the compiler (mostly) won't complain about.
Use integer arithmetic and build up the power incrementally. For example:
#include <iostream>
using namespace std;
int main()
{
for (int diff = 1; diff < 129; diff += 5)
{
int p = 1;
int j;
for (j = 0; p <= diff; j++)
{
p *= 2;
}
cout << diff << " = " << (j - 1) << '\n';
}
}
Sample output:
1 = 0
6 = 2
11 = 3
16 = 4
21 = 4
26 = 4
31 = 4
36 = 5
41 = 5
46 = 5
51 = 5
56 = 5
61 = 5
66 = 6
71 = 6
76 = 6
81 = 6
86 = 6
91 = 6
96 = 6
101 = 6
106 = 6
111 = 6
116 = 6
121 = 6
126 = 6
An alternative test strategy tests boundaries:
#include <iostream>
using namespace std;
int main()
{
int diff[] = { 1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65 };
int size = sizeof(diff) / sizeof(diff[0]);
for (int i = 0; i < size; i++)
{
int p = 1;
int j;
for (j = 0; p <= diff[i]; j++)
{
p *= 2;
}
cout << diff[i] << " = " << (j - 1) << '\n';
}
}
Sample output:
1 = 0
2 = 1
3 = 1
4 = 2
5 = 2
7 = 2
8 = 3
9 = 3
15 = 3
16 = 4
17 = 4
31 = 4
32 = 5
33 = 5
63 = 5
64 = 6
65 = 6
Clearly, the inside of the test loop should be wrapped into an antilog2 function — except for the printing operation.
My question looks like this
For numbers from 2 to 20, output the proper divisors and their sum.
So it should look like this:
2: 1 = 1
3: 1 = 1
4: 1+2 = 3
5: 1 = 1
6: 1+2+3 = 6
...
20: 1+2+4+5+10 = 23
This is what i wrote so far:
#include <iostream>
int main () {
int a=2;
int sum=1;
while (a<=10){
std::cout<<a<<": ";
for(int b=1; b<a; b=b+1)
{
if(a%b == 0)
{
std::cout<<b<<" + ";
sum+=b;}
if (b == a-1){
std::cout<<"= "<<sum<<"\n";
}
}
a++;
}
return 0;
}
When i compile and run, the output ends up looking like this:
2: 1 + = 2
3: 1 + = 3
4: 1 + 2 + = 6
5: 1 + = 7
6: 1 + 2 + 3 + = 13
7: 1 + = 14
8: 1 + 2 + 4 + = 21
9: 1 + 3 + = 25
10: 1 + 2 + 5 + = 33
My issues are currently:
Why does it give me the sum of all of the previous b results? I am trying to get the sum of only the divisors for each number. It gives me the sum of all previous sums.
Also, how do i get rid of the extra (+) at the end?
Many thanks!
EDIT:
Thanks guys! Here's the code after i adjusted it and cleaned it up a little bit!
#include <iostream>
int main() {
int a = 2;
while (a <= 20) {
std::cout <<a <<": ";
int sum = 0;
for (int b = 1; b < a; b = b + 1) {
if (a%b == 0) {
if (b == 1) {
std::cout <<b;
} else {
std::cout <<" + " <<b; }
sum += b; }
if (b == a-1) {
std::cout <<"= " <<sum <<"\n";
}
}
a++;
}
return 0;
}
It now works like a charm. The output has a few too many whitespaces but its good enough. Many thanks!
You need to reset the value of sum to zero at the beginning of each iteration of the while loop to avoid the multiple sum problem.
As far as the extra +, you could print "+" before the value of 'b' instead of after, and only if b > 1 (there is guaranteed a printed value before the current value of b).
Try something like this it uses this answer for not adding the + after the last item of divisors:
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
std::vector<int> properDivisors ( int number ) {
std::vector<int> divisors ;
for ( int i = 1 ; i < number / 2 + 1 ; i++ )
if ( number % i == 0 )
divisors.push_back( i ) ;
return divisors ;
}
int main()
{
for(size_t i = 2; i <= 20; ++i)
{
std::vector<int> divisors = properDivisors(i);
std::cout << i << ": ";
const char *padding = "";
for (auto iter = divisors.begin(); iter != divisors.end(); ++iter) {
std::cout << padding << *iter;
padding = " + ";
}
int sum = 0;
for (auto& n : divisors)
sum += n;
std::cout << " = " << sum;
std::cout << std::endl;
}
}
Output:
2: 1 = 1
3: 1 = 1
4: 1 + 2 = 3
5: 1 = 1
6: 1 + 2 + 3 = 6
7: 1 = 1
8: 1 + 2 + 4 = 7
9: 1 + 3 = 4
10: 1 + 2 + 5 = 8
11: 1 = 1
12: 1 + 2 + 3 + 4 + 6 = 16
13: 1 = 1
14: 1 + 2 + 7 = 10
15: 1 + 3 + 5 = 9
16: 1 + 2 + 4 + 8 = 15
17: 1 = 1
18: 1 + 2 + 3 + 6 + 9 = 21
19: 1 = 1
20: 1 + 2 + 4 + 5 + 10 = 22
Try it here!