Problem with finding successive integers in C - c

The main problem of the code is that finally the programm shows every time that there are not successive integers.
At first, I tried to figure out a solution to this problem, by investigating how to correct the "if" statement and then to fix some small mistakes on the code, but I was unable to find any error. The code is below
#include <stdio.h>
int main() {
int a,i;
int A[10];
for(i=0; i<=9; i++) {
scanf("%d",&a);
A[i]=a;
}
if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1)) {
printf("{%d,%d}",A[i+1],A[i]);
} else {
printf("Den yparxoun diadoxikoi arithmoi");
}
return 0;
}
Well, the expected result is to show, if exist, the successive integers as pairs. For example if I write the integers 4,-1,9,8,3,5,-21,6,7,8 the program should print {9,8}{6,7}{7,8}. The actual result is to show every time that there are not successive integers.
Thank you in advance for your help.

This should do it:
#include <stdio.h>
int main() {
int a,i;
int A[10];
int c =0;
for(i=0; i<=9; i++) {
scanf("%d",&a);
A[i]=a;
}
for(i=0;i<=9;i++)
{
if(A[i+1]==10)
{
break;
}
else if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1))
{
printf("{%d,%d}",A[i],A[i+1]);
c=1;
}
}
if(!c)
printf("Den yparxoun diadoxikoi arithmoi");
return 0;
}

You are supposed to use a loop in order to find out the pair by adding the loop your code looks like this
#include <stdio.h>
int main()
{
int a,i,flag=0;
int A[10];
for(i=0; i<=9; i++)
{
scanf("%d",&a);
A[i]=a;
}
for(i=0;i<9;i++){
if ((A[i+1]-A[i]==1)||(A[i+1]-A[i]==-1))
{
printf("{%d,%d}",A[i+1],A[i]);
flag=1;
}
}
if(!flag)
printf("Den yparxoun diadoxikoi arithmoi");
return 0;
}

Related

How can i fix this code in order to check if a number is palindrome, using pointers?

I am quite new to C. I don't really understand how to get the pointers right. I know it can be done without pointers , but I have to use them
#include <stdio.h>
#include <stdlib.h>
void palindrome(int *n)
{
int ok=0,*p,*m;
m=n;
while(*n!=0)
{
*p=*p*10+*n%10;
*n=*n/10;
}
if (*m==*p) ok=1;
if (*m!=*p) ok=0;
if (ok==1)
printf("Number is palindrome.");
if (ok==0)
printf("Number is not palindrome");
}
int main()
{
int n;
printf("Give value to n: ");
scanf("%d",&n);
palindrome(n);
}
The expected resut would be , for example, number 212 is palindrome, number 312 is not palindrome
Here is the code. I think you should try and understand how pointers work before actually using them. I didn't want to provide you the code, but since you are new, i am providing a working code. Try understand how it is working instead of just copy pasting this code to where ever you have to put.
#include <stdio.h>
#include <stdlib.h>
void palindrome(int *n)
{
int ok=0;
int *p = (int *)malloc(sizeof(int));
int *m = (int *)malloc(sizeof(int));
*p=0;
*m=*n;
while(*n!=0)
{
*p=*p*10+*n%10;
*n=*n/10;
}
if (*m==*p) ok=1;
if (*m!=*p) ok=0;
if (ok==1)
printf("Number is palindrome.");
if (ok==0)
printf("Number is not palindrome");
}
int main()
{
int n;
printf("Give value to n: ");
scanf("%d",&n);
palindrome(&n);
return 0;
}
#arpit-agrawal
You forgot to add free, at the end of
void palindrome(int *n)
{
...
free(p);
free(m);
}

Identify an Armstrong number

I Wrote this code which can identify whether a number is a Armstrong number or not
#include <stdio.h>
#include <stdlib.h>
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount()
{
int amount=0;
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
return amount;
}
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
When run with n=153 i always get 0.After several debugging,I found out the problem is somewhere in the Armstrong function(most likely)
int Armstrong()
{
n=input();
int v;
int z=0;
int y=10
int x=Num_amount();
int m[100]={};
int i;
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
return z;
}
The debug watches indicate that instead of execute the for loop,it went straight to the return z line,I have tried everything but still can't figure it out.Can you tell me what the problem is?
You are getting the wrong result because of some logical error. When you are choosing a variable to be global, you need to consider that the variable value can be modified by any function and in this case, you have already modified its value in num_amount function. You have also made some logical error in Num_amount and Armstrong function.
You haven't included math.h header file for pow.
Here is your modified code,
#include <stdio.h>
#include <stdlib.h>
#include<math.h> //<-------------Should have included
int n;
const int input()
{
printf("insert n:");
scanf("%d",&n);
return n;
}
int Num_amount() //<------------modified
{
int z = n; //<--------take a copy of global n
int amount=0;
while(z>0)
{
amount++;
z=z/10;
}
return amount;
}
int Armstrong() //<------------modified
{
n=input();
int v;
int z=0;
int x=Num_amount();
int i;
while(n>0)
{
v=n%10;
z+=pow(v,x);
n/=10; //<-------modification of global n
}
return z;
}
int main()
{
int z=Armstrong();
printf("%d",z);
}
Found a lot problems with the code. Here is a modified version.
1. Do not use a global variable.
2. Make calculation for power easier.
3. Return the status of result, not the result. You want to check whether number is Armstrong or not.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int no_digits(int n){
int digits = 0;
while(n){
digits++;
n = n/10;
}
return digits;
}
int armstrong(){
int n;
printf("insert n:");
scanf("%d",&n);
int digits = no_digits(n);
int curnum = 0,original = n;
while(n){
curnum += pow(n%10,digits);
n /= 10;
}
if(original == curnum)
return 1;
return 0;
}
int main(){
if(armstrong())
printf("Is Armstrong\n");
else printf("Not Armstrong\n");
}
Let's take a look at your loop:
for(i=0;n>=10;i++)
{
v=n%10;
m[i]=pow(v,x);
z=z+m[i];
y=y*10;
}
What's the value of n at this point? You've set it in the previous call to Num_amount like so:
while(n>=10)
{
amount++;
n=n/10;
if(n<10)
amount++;
}
So, after Num_amount has finished executing, n must be less than 10, meaning the loop in Armstrong won't execute.
This is a big reason why you shouldn't use globals, even in a toy program like this. If you use it for different purposes in different places, you just create headaches like this.
At the very least, you should change your code such that n is passed as a parameter to Num_amount and Armstrong.
Your function Num_amount() return "n" value is already less than 10 and for loop never run.

What's wrong with my program? Stuck in calculation loop or failed function call?

This is my program. I don't understand what's wrong in it. After I give the inputs nothing happens. I am guessing that the program got stuck in calculation loop or I am unable to call fact function in the loop. Ideas on where the problem should be?
#include<iostream>
#include<math.h>
using namespace std;
int fact(int a)
{
int f=0;
for(int i=1; i<=a;i++)
f=f*i;
}
main()
{
double x,temp1,temp2,sine;
int p,n;
temp1=temp2=0;
cout<<"Enter the value of x: ";
cin>>x;
cout<<"\nEnter the length of series: ";
cin>>n;
if(n%2==0)
{
for(p=1;p<=n-1;p+4)
{
temp1=temp1+(pow(x,p)/fact(p));
}
for(p=3;p<=n-1;p+4)
{
temp2=(temp2+pow(x,p)/fact(p))*(-1);
}
}
else
{
for(p=1;p<=n;p+4)
{
temp1=temp1+(pow(x,p)/fact(p));
}
for(p=3;p<=n;p+4)
{
temp2=-(temp2+pow(x,p)/fact(p));
}
}
sine=temp1+temp2;
cout<<"\nsinx= "<<sine;
}
What is p+4 doing in the increment section of the for loop? Also you aren't returning any values from int fact().
check this
int fact(int a)
{
int f=1;
for(int i=1; i<=a;i++)
f=f*i;
}

Removing Duplicate values and adding 0 on their place

I have written a code to remove duplicate values and add 0 on there place .
But i feel like my code should be much better than this,if anybody can give a better idea of developing this code.
Please suggest me and advice me.
Input--2,3,4,3,6
output--2,3,4,0,6
Here is my code:
#include<stdio.h>
int main()
{
int a[100],b[100];
int i,j,size;
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<size;i++)
{
b[i]=a[i];
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(a[i]==a[j])
{
b[j]=0;
}
}
}
for(i=0;i<size;i++)
printf("%d\n",b[i]);
return 0;
}
Clear duplicates as they are entered as follows, comparing with the values entered so far:
#include<stdio.h>
int main() {
int a[100];
int i,j,size;
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
for(j=0;j<i;j++){
if(a[j]==a[i]) {
a[i]=0; /* found duplicate among previous entries! */
break;
}
}
}
for(i=0;i<size;i++)
printf("%d\n",a[i]);
return 0;
}
Here is some of my idea:
Solution 1:
Use bit to indicate whether a number occurred yet.
e.g On 32bit machine, 1 int has 32bit, if your number range is 1 ~ 1000, then you need 32 int, you can change it range when you met a larger number, by realloc().
If your number range is small, then it's quite suitable.
Solution 2:
Store sorted numbers in a binary tree, so that you can search quicker.
You can use the single array itself, and mark at the positions where you find a duplicate in it. Something like this.
#include<stdio.h>
int main() {
int a[100];
int i,j,size;
scanf("%d",&size);
for(i=0;i<size;i++) {
scanf("%d",&a[i]);
}
for(i=1; i<size; i++) {
for(j=i-1; j>=0; j--) {
if(a[i]==a[j]) {
a[i]=0;
break;
}
}
}
for(i=0;i<size;i++)
printf("%d ",a[i]);
return 0;
}
Here's an improvement, but I agree it feels like it can be significantly better (Maybe I'll get back to this later...):
#include<stdio.h>
int main()
{
int a[100];
int i,j,size,left;
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
left = size;
for(i=0;i<size&&left>1;i++) // If there's only 1 left, it's not a duplicate
{
if(a[i] == 0) // No need to test these, already done
continue;
for(j=i+1,left=0;j<size;j++)
{
if(a[i]==a[j])
{
a[j]=0;
}
if(a[j]!=0)
left++; // If we don't get here, there's nothing left to test
}
}
for(i=0;i<size;i++)
printf("%d\n",a[i]);
return 0;
}
So basically, don't search for 0's ahead of current position, and when searching for anything else count (mark actually) if there is anything left to test.

Time limit exceeded error. what does it mean?

I want to write a C program that prints the maximum of 10 integer numbers. but i get this error. what is the problem?(error is: time limit exceeded)
int main()
{
int arr[9];
int i;
int max=-1;
for(i=0;i<=10;i++) {
scanf("%d",&arr[i]);
if(arr[i]>arr[i+1]){
arr[i]=max;
}
}
printf("%d",max);
}
program works like that,thank u for help
int main()
{
int arr[9];
int i;
int max=-1;
for(i=0;i<=9;i++) {
scanf("%d",&arr[i]);
if(arr[i]>max) {
max=arr[i]; } }
printf("%d",max);
}
you are indexing two past the end of the array. you need to make the for condition this:
for(i=0;i<9;i++)

Resources