Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I'm very new to c++ and everything in general. I dont understand why my code doesn't work:
#include <cstdio>
int main(){
int a;
scanf("%d", &a);
for(int i = 1; i < a; i++){
printf(" ");
}
printf("*");
}
what I'm trying to do is make the number of spaces as the value inputted by the user minus and add a * at the end so if a = 10 :
*
but when I try it the spaces don't show.
Ideally what I'm trying to do is get an output something like:
*
**
***
****
The length depends on user input if it was 10 then the spaces would be 9 and the * at the end would be the tenth.
You will have to use 3 for-loops (two of them nested inside one). The parent for loop for each row. And the nested two are for spaces and stars.
#include <cstdio>
int main()
{
int a;
scanf("%d", &a);
for(int i=1; i<a; i++)
{
for(int j=i; j<a; j++)
printf(" ");
for(int k=0; k<i; k++)
printf("*");
printf("\n");
}
return 0;
}
You will get your desired output if you put the value 5 for a, i.e., a=5.
To learn more in detail, you can follow this link star patterns in c
Your code may contain many errors. That's understandable, cause you are new to C/C++. Try problem-solving in beecrowd to improve your problem-solving capabilities and learn C/C++ in a better way. These errors will automatically go away day by day as you progress. But the secret is to practice and never give up. Good luck!
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Problem: https://www.hackerrank.com/challenges/migratory-birds/problem
Test Case Link: https://hr-testcases-us-east-1.s3.amazonaws.com/33294/input04.txt?AWSAccessKeyId=AKIAJ4WZFDFQTZRGO3QA&Expires=1573162301&Signature=MgpSHa3lxX%2FwYwumjzAmF8uviZE%3D&response-content-type=text%2Fplain
for this test case i cant get any output why ?
Thanks in advance
#include<iostream>
using namespace std;
int main()
{
long long n,i=0,num=0,mx=0,r=0;
cin>>n;
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
for(int i=1;i<6;i++)
{
if(arr[i]>mx)
{
mx=arr[i];
r=i;
}
}
cout<<r;
}
This looks problematic:
long long arr[6]{0};
for(int i=0;i<n;i++)
{
cin>>num;
arr[num]++;
}
If the input value read into num is greater than 5 (or less than zero), undefined behavior will exist when a value is written into an invalid memory location offset from arr.
There is nothing wrong with you code i got accepted all test cases. But the solution can be return in more optimized way
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
How do I improve this spaghetti code? I am a student and this is me being slow in my first programming course.
What I think I want to achieve
Find an alternating sum up to a number.
Problem overview
https://www.hackerrank.com/contests/cs102-s18-march31/challenges/treasure-road
This is a problem from the archive at provided by the teacher. I have deduced that an alternating sum has to be calculated, where the sum of the first odd integers has to be subtracted from the sum of the first even integers (of course up to #ofsteps/2).
What I did
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int arrodds[100]={0},arrevens[100]={0},sumevens=0,sumodds=0;
int steps;
cin >> steps;
int i=0;
while(i<steps/2)
{
if(i%2==0)
arrevens[i]=i;
else arrodds[i]=i;
i++;
}
for(int d=0;d<100;d++)
sumevens+=arrevens[d];
for(int h=0;h<100;h++)
sumodds+=arrodds[h];
cout << sumodds-sumevens;
return 0;
}
My logic/reasoning
Zingo can only move Steps/2, if he moves an odd step he moves forward, and Ringo moves a step closer to to Zingo, else if Zingo moves an even step Ringo moves away from Zingo.
Let's say there are 10 steps... 10/5=5... 5 steps= 1+2+3+4+5, odd steps=1,3,5; even steps=2,4; so displacement is 1+3-(2+4)=-2.
The code
I try to store the even and odd numbers in different arrays, then take the sum of each one and subtract the sums.
What's wrong?
My code works for small numbers it seems, as some test cases go beyond what I think My code could handle.
What I tried
I tried playing around with the sizes of my arrays but didn't get what I want.
Should I abandon this code and try something else? If not, how can I fix this abomination and guide me through it?
You can use a running sum instead of using arrays:
sum_evens = 0;
sum_odds = 0;
for (int i = 0; i < steps; ++i)
{
if (i & 1)
{
sum_odds += i;
}
else
{
sum_evens += i;
}
}
Edit 1:
The sum of the first N odd numbers is N*N.
You could change the calculation to:
sum_odds = steps * steps;
I'll leave the calculation of the first N even numbers up to the reader / OP.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
1st code: works fine gives success with time of 0sec
int main()
{
int n=100000;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{}
cout<<"ffdfdf";
}
2nd code: gives a time limit exceeded
int main()
{
int n=100000;
bool **a=new bool*[n];
for(int i=0;i<n;i++)
{
bool[i]=new bool[n];
for(int j=0;j<n;j++)
{
bool[i][j]=1;
}
}
cout<<"ffdfdf";
}
can anyone explain why the two code fragments have a vast time difference.I am not understanding it.
bool[i]=new bool[n]; is extremely expensive cf. the other statements.
A good compiler will optimise out your first program to cout << "ffdfdf";, since it will know that the loop doesn't do anything.
Once you've replaced your errant bools with as so the second snippet actually compiles, you'd be advised to pair your new[] calls with delete[].
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
The program will ask how many items to store, and then asks for the name of the item and the price.
Finally, all the items must be displayed in a table. Duplicate items must be displayed as one item. Use only small letters.
Display should look like this:
My problem #1 is that if an item with its price is equal to another input, the program should count them as 1 and the quantity of an item will depend on how many same input(the item and price).
Problem #2 is that the 1st for loop in the void function does not work properly, it always ask an item and price and doesn't stop, (#3) and the item number too is not working properly. Here's the picture:
Here is my code:
#include<iostream>
using namespace std;
int num;
void numItems(int num){
string items[num];
double price[num];
int quantity[num];
for(int x=0;x<num;x++){
cout<<"Item #"<<x+1<<": ";
cin.get();//cin.ignore();
/*cin>>items[x];*/getline(cin,items[x]);
cout<<"Price #"<<x+1<<": ";
cin>>price[x];
if(x=1){ //(x==1) fixed
if(items[x]==items[x-1] && price[x]==price[x-1]){
quantity[x]++;
}
}
cout<<"\n\n";
}
cout<<"============RECEIPT=============\n\n";
cout<<"Items\t\t\t\tPrice\t\t\t\tQuantity"<<endl;
for(int x=0;x<num;x++){
if(x=1){
if(items[x]==items[x-1]){
cout<<items[x]<<"\t\t\t\t"<<price[x]<<"\t\t\t\t"<<quantity<<endl;
}else continue;
}
}
}
int main(){
cout<<"===ITEM AUDIT==="<<endl;
cout<<"Enter number of items to store: ";
cin>>num;
cout<<"\n\n";
numItems(num);
}
The if statement has a hard-to-spot issue:
if(x=1){
should be
if(x == 1) {
Don't forget to fix both typos!
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.