#include <stdio.h>
int main() {
int M,N,i=0,sum=0;
while(M>0 || N>0){
scanf("%d%d",&M,&N);
if(M<=0 || N<=0){
break;
}
else if(M==N){ printf("%d Sum=%d\n",N,M); }
else if(M<N){
for(i=M; i<=N; i++){
printf("%d ",i);
sum+=i;
}
printf("Sum=%d\n",sum);
sum=0;
}
else if(M>N){
for(i=N; i<=M; i++){
printf("%d ",i);
sum+=i;
}
printf("Sum=%d\n",sum);
sum=0;
}
}
return 0;
}
what is wrong with this code? URI judge saying 100% wrong answer but I tried all the test cases and it passed in those cases
I'm not sure, but before while loop you should get scanf("%d%d",&M,&N); one time.
Because the m and n is not defined.
Like this way :
scanf("%d%d",&M,&N);
while(M>0 || N>0){
scanf("%d%d",&M,&N);
if(M<=0 || N<=0){
break;
}
...
Related
I tried to implement insertion sort in C using while and for loop as follows:
#include <stdlib.h>
int main()
{
int a[]={4,7,8,2,16,21,12,3,1};
int n=sizeof(a)/sizeof(a[0]);
int j,k,i,x;
/*for(j=0;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0 && k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k;
}*/
for(i=1;i<n;i++)
{
k=a[i];
j=i-1;
while(j>=0 && a[j]>k)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=k;
}
printf("The elements of the array are: ");
for(x=0;x<n;x++)
printf("%d ", a[x]);
return 0;
}
And it works totally fine. But, When I tried to remove the use of variable 'k'. The answer prints as follows:
My latter code as follows:
#include <stdlib.h>
int main()
{
int a[]={4,7,8,2,16,21,12,3,1};
int n=sizeof(a)/sizeof(a[0]);
int j,k,i,x;
/*for(j=0;j<n;j++)
{
k=a[j];
for(i=j-1;i>=0 && k<a[i];i--)
a[i+1]=a[i];
a[i+1]=k;
}*/
for(i=1;i<n;i++)
{
//k=a[i];
j=i-1;
while(j>=0 && a[j]>a[i])
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=a[i];
}
printf("The elements of the array are: ");
for(x=0;x<n;x++)
printf("%d ", a[x]);
return 0;
}
Can somebody explain this?. Does anything changes the value of the variable 'i' during while loop.
k=a[i] saves the value because the while loop
while (...) {
a[j+1] =a[j];
}
overwrites a[i] at first iteration, because j starts at i-1 so j+1 is i at the first iteration.
I want to show prime numbers between two numbers like this 2,5,7,11
but it shows like this 2,5,7,11, there is an extra ",".
#include <stdio.h>
int main()
{
int n1,n2,f,i,j;
scanf("%d %d", &n1, &n2);
for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f==0)
if(i!=1)
printf("%d,",i);
}
return 0;
}
Answering the question but not accounting for the point raised by #Bathsheeba:
#include <stdio.h>
int main()
{
int n1,n2,f,i,j;
scanf("%d %d", &n1, &n2);
int itemCount = 0; // NEW
for(i=n1; i<n2; ++i)
{
f=0;
for(j=2; j<=i/2; ++j)
{
if(i%j==0)
{
f=1;
break;
}
}
if (f == 0 && i != 1) // TESTS COMBINED
{
if (itemCount++ > 0) putchar(','); // HERE
printf("%d",i); // COMMA REMOVED
}
}
printf("\n"); // newline at the end
return 0;
}
The only way to really remove a comma after you've added it is if you're building a buffer at runtime - which is a reasonable approach - so here we have to only generate a comma when we need it, before all but the first item.
I'm scratching my head from the morning and I didn't even reach something which looks like given pattern. Any amount of help is appreciated.
Thanks in advance!
This is not my home work
EDIT 1: Thank You all! I finally arrived at the solution.
`#include<stdio.h>
void main(){
int n,i,count;
scanf("%d",&n);
int prev=n,next=(n*2)+(n-2),tc=1;
for(int i=1;i<=n;i++){
if(i==1) {
for(count=1;count<=(n*2)+(n-2);count++) {
if(count==prev || count==next) printf("*");
else printf(" ");
}
}
else {
for(count=1;count<=2*n*n;count++){
if(count==prev-tc || count==prev+tc || count==next-tc|| count==next+tc)
printf("*");
else printf(" ");
}
tc++;
}
printf("\n");
}
}`
to solve questions of this type you must find a formula for spaces and a formula for stars. these formulas tell the computer how many spaces and stars must be printed in each line.
that's all these types of questions need.
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
for(int x=n;x>=i;x--){
printf(" ");
}
int y=2*i; int f=1;
while(y!=1){
if(f!=1&&f!=2*i-1){
printf(" ");
}
else if(i==n&&f==2*i-1){
printf("");
}
else{
printf("*");
}
f++;
y--;
}
if(i==n){
printf("*");
}
for(int xk=n;xk>i;xk--){
printf(" ");
}
int y1=2*i; int f2=1;
while(y1!=1){
if(f2==2*i-1){
printf("");
}
if(f2!=1&&f2!=2*i-1){
printf(" ");
}
else if(i==n&&f2==1){
printf(" ");
}
else{
printf("*");
}
f2++;
y1--;
}
printf("\n");
}
}
I have just completed a simple array operation in C. I have done the programming consisting of a number of functions all performing the insert operation at various locations. The main problem that I'm facing is that the control is not returning to main after I have performed the operation with a function . I can't find the reason. Just explain why is this happening and provide me with a proper solution. My code is like this:
//To insert an element into an already formed array
#include<stdio.h>
#include<stdlib.h>
int binsert(int[],int);
int minsert(int[],int,int);
int einsert(int[],int);
void print(int[],int);
void main()
{
int a[100],i,j,l,loc;
printf("Enter the length of the array\n");
scanf("%d",&l);
printf("Enter the numbers in the array\n");
for(j=0;j<l;j++)
{
scanf("%d",&a[j]);
}
printf("Enter the following options for the operation\n1:insert at the beginning\n2:insert at specific location\n3:insert at the end\n4:print the array\nAny other key to end the operation\n");
scanf("%d",&i);
do
{
switch(i)
{
case 1:
l=binsert(a,l);
break;
case 2:
printf("Enter the location at which you would want to insert(location starts from 1)\n");
scanf("%d",&loc);
l=minsert(a,loc,l);
break;
case 3:
l=einsert(a,l);
break;
case 4:
print(a,l);
break;
}
}while(i==1 || i==2 || i==3 || i==4);
printf("Operation terminated\n");
exit(0);
}
int binsert(int a[],int l)
{
int i;
for(i=l-1;i>=0;i--)
{
a[i+1]=a[i];
}
printf("Enter the number to insert\n");
scanf("%d",&a[0]);
return (l+1);
}
int minsert(int a[],int loc,int l)
{
int i;
for(i=l-1;i>=loc;i--)
{
a[i+1]=a[i];
}
printf("Enter the number to insert\n");
scanf("%d",&a[loc]);
return (l+1);
}
int einsert(int a[],int l)
{
printf("Enter the number to insert\n");
scanf("%d",&a[l+1]);
return (l+1);
}
void print(int a[],int l)
{
int i;
printf("The values in the array are as follows\n");
for(i=0;i<l;i++)
{
printf("%d\n",a[i]);
}
}
PROBLEM 1 :
you should have done :
do
{
printf("Enter the following options for the operation\n1:insert at the beginning\n2:insert at specific location\n3:insert at the end\n4:print the array\nAny other key to end the operation\n");
scanf("%d",&i);
switch(i)
{
....
}
}while(i==1 || i==2 || i==3 || i==4);
otherwise , i will always contain the same value i.e among 1 , 2, 3, 4, and while(i==1 || i==2 || i==3 || i==4); will always be true , and so same function will be called indefinitely.
PROBLEM 2: have a check on l , so that it do not exceed 100 , size of your array .
What you think?
//To insert an element into an already formed array
#include<stdio.h>
#include<stdlib.h>
int binsert(int[],int);
int minsert(int[],int,int);
int einsert(int[],int);
void print(int[],int);
void main()
{
int a[100],i,j,l,loc;
for(i=0; i < 100; i++)
a[i] = -1;
printf("Enter the length of the array\n");
scanf("%d%*c",&l);
for(j=0; j<l; j++)
{
system("cls");
printf("Enter the numbers in the array\n");
for(i = 0; i < j; i++)
printf("%d ", a[i]);
scanf("%d%*c",&a[j]);
}
do
{
system("cls");
printf("Array: ");
for(j = 0; j < (sizeof(a)/sizeof(a[0])) && a[j] != -1; j++)
printf("%d ", a[j]);
printf("\n\n\nEnter the following options for the operation\n\n\t1:insert at the beginning\n\t2:insert at specific location\n\t3:insert at the end\n\t4:print the array\n\tAny other key to end the operation\n\nOption: ");
scanf("%d%*c",&i);
switch(i)
{
case 1:
l=binsert(a,l);
break;
case 2:
printf("Enter the location at which you would want to insert(location starts from 1)\n");
scanf("%d",&loc);
l=minsert(a,loc,l);
break;
case 3:
l=einsert(a,l);
break;
case 4:
print(a,l);
break;
}
}
while(i==1 || i==2 || i==3 || i==4);
printf("\n\nOperation terminated\n");
getchar();
exit(0);
}
int binsert(int a[],int l)
{
int i;
for(i=l-1; i>=0; i--)
{
a[i+1]=a[i];
}
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[0]);
return (l+1);
}
int minsert(int a[],int loc,int l)
{
int i;
for(i=l-1; i>=loc; i--)
{
a[i+1]=a[i];
}
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[loc]);
return (l+1);
}
int einsert(int a[],int l)
{
printf("\n\nEnter the number to insert\n");
scanf("%d",&a[l+1]);
return (l+1);
}
void print(int a[],int l)
{
int i;
printf("\n\nThe values in the array are as follows\n");
for(i=0; i<l; i++)
{
printf("%d\n",a[i]);
}
}
I think below line is causing problem
for(i=l-1;i>=0;i--)
{a[i+1]=a[i];
}
It's like when i becomes 0 it will execute and then i-- will cause it to be -1 and negative numbers are stored in 2's complement so it will be positive number and condition i>=0 is satisfied every time.
Hope this might help you
Thanks
I wrote a piece of code which attempts to tell which of three user-inputted numbers is the greatest. However, I am unable to understand why my code breaks for the input 3, 1, 2 and works for the input 55, 54, 56.
My code:
main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest",a);
if(b>a && b>c)
printf("%d is greatest",b);
else printf("%d is greatest",c);
getch();
}
What am I doing that causes this error, and what can I do to fix it?
You are missing "else if", that's for sure.
main()
{
int a,b,c;
printf("enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest",a);
else if(b>a && b>c)
printf("%d is greatest",b);
else
printf("%d is greatest",c);
}
You need to add an else before the line if(b>a && b>c).
i.e.
if(b>a && b>c)
should be
else if(b>a && b>c)
What people say here are true, but for best practice I would reduce the checks in the ifs:
main()
{
int a,b,c;
printf("enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if(a>=b) //it's not b.
{
if(a>=c)
{
printf("%d is greatest",a);
}
else
{
printf("%d is greatest",c);
}
}
else // here you know that b > a, then it's not a.
{
if(b>=c)
{
printf("%d is greatest",b);
}
else
{
printf("%d is greatest",c);
}
}
}
try this
condition? exp1: exp2;
evaluates to
if condition is true then return exp1 else return exp2
int main(){
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
int d = (a >= b)? a: b;
d = (d >= c)? d: c;
printf("%d is greatest", d);
}
Your second if statement should be else if
Just add a simple else if statement to your code and it should work fine as in :
main()
{
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest\n",a);
else if(b>a && b>c)
printf("%d is greatest\n",b);
else printf("%d is greatest\n",c);
//getch();
}
Why don't you try this. Its neater. The problem with your code was that you were missing an else which could have been put along with if.
main() {
int a,b,c;
printf("enter three numbers");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
printf("%d is greatest\n",a);
else if(b>c)
printf("%d is greatest\n",b);
else printf("%d is greatest\n",c);
//getch();
}
#define MAX(a,b) (((a)>=(b))?(a):(b))
#define MAX3(a,b,c) MAX(MAX(a,b),c)