I thought this would be easier than originally planned. I'm trying to make this image out of nested for loops:
Any suggestions or solutions would be helpful.
#include <iostream>
using namespace std;
int main()
{
for(int i=0; i<1;i++)
{
cout<<i+1<<endl;
for(int j=0;j<2;j++)
{
cout<<j+1;
}
}
cout<<"\n";
for(int k=0; k<3; k++)
{
cout<<k+1;
}
cout<<"\n";
for(int l=0; l<4; l++)
{
cout<<l+1;
}
cout<<"\n";
for(int m=4; m>0; m--)
{
cout<<m;
}
cout<<"\n";
for(int n=3; n>0; n--)
{
cout<<n;
}
cout<<"\n";
for(int o=2; o>0; o--)
{
cout<<o;
}
cout<<"\n";
for(int p=0; p<1; p++)
{
cout<<p+1;
}
cin.get();
return 0;
}
Here's a solution in C for you =)
#include <stdio.h>
#include <string.h>
int main(void) {
char forward[5] = "1";
char reverse[5] = "4321";
int i;
for( i = 1; i <= 4; i++ ) {
printf( "%s\n", forward );
forward[i] = forward[i-1]+1;
}
for( i = 0; i < 4; i++ ) printf( "% 4s\n", reverse+i );
}
If you want an extremely compact and not very follow-able solution I decided to give it a shot.
int length = 4;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < length; j++)
{
for(int k = 0; k < (i == 1 ? length - j : j + 1); k++)
{
if(i == 1 && k == 0)
for(int x = 0; x < j; x++)
cout << " ";
cout << (i == 1 ? (length - k) - j : k + 1);
}
cout << endl;
}
}
Where length is the number of iterations from 1 to length.
Ok, since paddy posted his C solution (and someone said "compact" =P)...
#include <stdio.h>
int main()
{
char line[] = "1234321";
int i=0;
for (; i<4; printf("%.*s\n",++i,line));
for (i=0;i<4; printf("%4s\n",line+3+i++));
return 0;
}
Output
1
12
123
1234
4321
321
21
1
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
/*first half: From level1 = 1 to less than 5, level2 = 1 to less than or equal to level1, print level2 */
for(int level1 = 1; level1 < 5; level1++) {
for(int level2 = 1; level2 <= level1; level2++){
cout << level2;
}
cout << '\n'; /*prints the newline AFTER each iteration of the `level2` loop*/
}
/*second half: reverse the logic of part1, but also add spaces in the beginning */
for(int level1 = 4; level1 > 0; level1--){
for(int interim = 4; interim > level1; interim--) cout << ' ';
for(int level2 = level1; level2 > 0; level2--){
cout << level2;
}
cout << '\n';
}
return 0;
}
Let me know if you do not understand this code :-)
Related
image of shape
I have no issues with first 4 rows. 5th row is the problem. I am required to use loops but dont know how i am suppose to print 6 (-.*) with 0 spaces when all rows above follow a pattern.
Something like this should work for you
for (int i = 1; i <= 6; i++) {
for (int j = 28 - i * 3; j >= 0; j--) {
std::cout << " ";
}
for (int j = 0; j < i; j++) {
std::cout << "-.*";
}
std::cout << std::endl;
if (i == 4) i++;
}
Basically check when you are on the 4th row and just have it skip a row by incrementing your row index loop.
Something like this?
std::string repeat(std::string s, int n) {
std::string repeat;
for (int i = 0; i < n; ++i)
repeat += s;
return repeat;
}
int main()
{
int PADDING = 20;
int MAX = 7;
for (int i = 1; i < MAX; ++i) {
if (i == 5) continue;
std::string padding(3*(PADDING-MAX-i), ' ');
std::cout << padding << repeat("-.*", i) << std::endl;
}
}
Output:
-.*
-.*-.*
-.*-.*-.*
-.*-.*-.*-.*
-.*-.*-.*-.*-.*-.*
Live demo:
http://cpp.sh/6mtpx
In C (not C++):
#include <stdio.h>
int main(void) {
int height = 5;
char txt[3*height+1];
char sym[]= "-.*";
for(int i=0; i<3*height; ++i) txt[i]=sym[i%3];
txt[3*height]=0;
for(int i=0; i<height; ++i)
printf("%s%*.*s\n", (i+1==height)? &txt[3*(height-1)] : "", 3*(height+(i+1<height)), (i+1)*3, txt);
return 0;
}
Output:
Success #stdin #stdout 0s 5476KB
-.*
-.*-.*
-.*-.*-.*
-.*-.*-.*-.*
-.*-.*-.*-.*-.*-.*
IDEOne Link
I need to enter 2 integers, n and k, n is the range and k is the amount of divisors for those numbers, and only display the numbers with k divisors.
#include <iostream>
using namespace std;
int main()
{
int n, k,cnt=0;
cin>>n;
cin>>k;
for(int i=1; i<=n; i++)
{
if(n%i==0)
{
cnt++;
}
if(k==cnt)
cout<<i<<" ";
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, k;
cin>>n>>k;
for(int i = 1; i <= n; i++){
int num_divisors_i = 0;
for (int j = 1, len = sqrt(i); j <= len; j++) {
if (i % j == 0) {
if (i / j == j) {
num_divisors_i++;
}
else {
num_divisors_i = num_divisors_i + 2;
}
}
}
if(num_divisors_i == k) cout<<i<<" has "<<k<<" divisors"<<endl;
}
}
This was my solution:
#include<iostream>
int main(int argc, char* argv[]) {
int n = 0, k = 0;
std::cout << "Enter the range: ";
std::cin >> n; if (!std::cin) throw std::runtime_error("range read failed");
std::cout << std::endl << "Enter the number divisors: ";
std::cin >> k; if (!std::cin) throw std::runtime_error("number of divisors read failed");
std::cout << "numbers with " << k << " divisor:: ";
for (int i = 1; i != n+1/*if k is inclusive else n*/; ++i) {
int cnt = 0;
for (int j = 1; j != i+1; ++j) {
if (i % j == 0)
cnt += 1;
}
if(cnt == k)
std::cout << i << ' ';
}
return 0;
}
1
3 2
6 5 4
10 9 8 7
I want to print the following pattern. I have tried very hard but couldn't make the code for it. I have tried everything which came up to my mind.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1;i <= n; i++) {
for (j = 1; j <= i; j++) {
k++;
printf("%d ", k);
}
printf("\n");
}
}
the other code which i tried is this.
#include<iostream>
using namespace std;
int main() {
int i, j, n;
cin >> n;
int k = 0;
for (i = 1; i <= n; i++) {
for (j = i; j >= 1; j--) {
k++;
printf("%d ",j);
}
printf("\n");
}
}
#include <iostream>
#include <stack>
using namespace std;
int main()
{
int previousRow = 0;
for(int row = 1; row <= 4; row++)
{
int rowTracker = row;
for(int col = 0; col < row; col++)
{
cout<<rowTracker - col + previousRow<<" ";
}
previousRow += row;
cout<<endl;
}
return 0;
}
#include<iostream>
void printPattern(unsigned numlevels)
{
unsigned last_num = 1;
for(unsigned i = 0; i < numlevels; ++i)
{
unsigned next_num = i + last_num;
for(unsigned j = next_num; j >= last_num; --j)
{
std::cout << j << ' ';
}
std::cout << '\n';
last_num = next_num + 1;
}
}
int main()
{
unsigned n;
std::cin >> n;
printPattern(n);
return 0;
}
You may also use a stack to implement this. Here is a working answer:
#include <iostream>
#include <stack>
using namespace std;
int main() {
int i, j, n;
stack<int> st;
cin >> n;
int k = 0;
for(i = 1;i <= n; i++) {
for(j = 1; j <= i; j++) {
k++;
st.push(k);
}
while(!st.empty()){
printf("%d ", st.top());
st.pop();
}
printf("\n");
}
}
Hope it helps!
Before reading the code blow, you should really try to do it yourself. This problem is obviously for practice and to develop the programming muscle. Just getting the answer is not going to help.
The issue with your code is that for each row, the range you want to print is not being determined correctly. You should first find the range and then print the numbers. Ther can be multiple approaches to it. Below is one of them.
for(i=1;i<=n;i++){
int max = i*(i+1)/2;
int min = i*(i-1)/2 + 1;
for(j=max;j>=min;j--){
printf("%d ",j);
}
printf("\n");
}
Here is a simple method
int main(int argc, char* argv[])
{
int n = 4; // suppose print 4 lines
for (int i = 1; i <= n; ++i)
{
int i0 = (i + 1) * i / 2; // first number of line i
for (int j = 0; j < i; j++)
cout << i0 - j << " ";
cout << endl;
}
return 0;
}
thanx everyone for your responses. I was able to do it on my own. Below is what I did. if there is any correction let me know
#include<iostream>
using namespace std;
int main(){
int i,j,n,temp;
cin>>n;
int k=0;
for(i=1;i<=n;i++){
k=k+i,temp=k;
for(j=1;j<=i;j++){
cout<<temp<<+" ";
temp--;
}
cout<<("\n");
}
}
I tried to answer write a code to solve this problem, but I'm still getting a wrong answer at test 15 and I don't know what is missing in my code.
I tried a lot of test cases but the code has solved them all correctly.
My Code :
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
int c; cin >> c;
int v; cin >> v;
if (c == 1 && v == 0)
{
cout << 1 << " " << 1;
}
else
{
int cArray[c + 1];
int voting[v][c];
for (int j = 0; j<v; j++)
{
for (int z = 0; z<c; z++)
{
int temp; cin >> temp;
voting[j][z] = temp;
}
}
for (int j = 0; j <= c; j++)cArray[j] = 0;
for (int j = 0; j<v; j++)cArray[voting[j][0]]++;
int maxim = 0;
int maxN = 0;
int count = 0;
map<int, int > cand;
for (int j = 1; j <= c; j++)
{
if (cArray[j]>maxN)
{
cand.clear();
cand[j] = 1;
maxN = cArray[j];
maxim = j;
count = 0;
}
else if (cArray[j] == maxN)
{
cand[j] = 1;
count++;
}
}
if (count == 0)
cout << maxim << " " << 1;
else
{
for (int j = 0; j<v; j++)
{
for (int z = 1; z<c; z++)
{
if (cand.count(voting[j][z]))
{
cArray[voting[j][z]]++;
break;
}
}
}
maxim = 0;
maxN = 0;
count = 0;
for (int j = 1; j <= c; j++)
{
if (cArray[j]>maxN)
{
maxN = cArray[j];
maxim = j;
count = 0;
}
else if (cArray[j] == maxN)
{
count++;
}
}
cout << maxim << " " << 2;
}
}
return 0;
}
Your algorithm for checking the first round (win or top two candidates) seems wrong. It looks like you are expecting the top two candidates to have the same number of primary votes - this is not the case. You want to pick the top two candidates and the top one wins if it has more than 50 % of the vote.
I don't want to give you the answer (as that is the point of doing the exercises), but you need to rethink how you are processing the first part of the vote.
Also note that once someone has voted for one of the top two candidates, their secondary votes should not then count toward the other candidate (which you are currently doing).
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
}
}