Time limit exceeded error. what does it mean? - c

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++)

Related

program doesn't read a file properly

For homework I have to write a program where I am typing the string of n integer and then the program that prints the string,calculates the arithmetic mean and makes a new file only with even numbers and at the end prints the new file on screen.
And here are 2 programs
#include<stdio.h>
main()
{
int x,n,i;
FILE *p;
p=fopen("podaci.dat","wb");
printf("n=");
scanf("%d",&n);
fwrite(&n,sizeof(int),1,p);
for(i=0;i<n;i++)
{
printf("x=");
scanf("%d",&x);
fwrite(&x,sizeof(int),1,p);
}
fclose(p);
}
#include<stdio.h>
void stampa(int n,int a[])
{
int i;
for(i=0;i<n;i++)
printf("%5d",a[i]);
printf("\n");
}
float ars(int n,int a[])
{
int i,s=0;
float ars=0;
for(i=0;i<n;i++)
s+=a[i];
return 1.0*s/n;
}
main()
{
int i,n;
FILE *p,*u;
u=fopen("niz.dat","wb");
p=fopen("podaci.dat","rb");
fread(&n,sizeof(int),1,p);
printf("n=%d\n",n);
int a[n],m=0;
for(i=0;i<n;i++)
{
fread(&a[i],sizeof(int),1,p);
if(a[i]%2==0)
{
m+=1;
}
fwrite(&m,sizeof(int),1,u);
for(i=0;i<n;i++)
if(a[i]%2==0)
{
fwrite(&a[i],sizeof(int),1,u);
}
stampa(n,a);
printf("ars=%.2f",ars(n,a));
fclose(p);
fclose(u);
}
}
When I type more than 2 numbers the program instead of those numbers reads them as 0 and sometimes it adds numbers.
You cannot declare an array a[n] where n is a variable (number that you don't know at compile time but only once you read the file).
You can ether declare a[N] where N is a number big enough defined in with #DEFINE
#include <stdio.h>
#include ...
#DEFINE N 10000
//...
int a[N];
or you have to allocate a[] dynamically
int * a;
a = (int*)malloc(sizeof(int)*n);
You can read more about this here and here

Problem with finding successive integers in 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;
}

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;
}

C program keeps crashing

My program keeps crashing. The codes seems legit and correct. Wonder what's the problem.
#include <stdio.h>
void queue(int length,int turns){
int permutations,totalTurns;
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int permutations=0;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}
int permutations=0;
average=totalTurns/permutations;
You're dividing by zero.
Note that the permutations variable you've declared in main() is different from the one in queue().
You should probably return the permutations value from queue(), like this:
int queue(int length,int turns){
int permutations = 0;
...
return permutations;
}
int main(void) {
...
int permutations = queue(length,-1);
}
You should declare permutations as a global variable, i.e. outside main function as well as totalTurns because as others mentioned it's always 0 because even thought you declare it in function queue it's being forgotten outside it.
#include <stdio.h>
static int permutations=0;
static int totalTurns=0;
void queue(int length,int turns){
turns++;
if (length>0){
queue(length-1,turns);
if (length>1){
queue(length-2,turns);
}
}
else{
permutations++;
totalTurns+=turns;
}
}
int main()
{
while(true){
int length;
float average;
int totalTurns=0;
printf("Queue length: ");
scanf("%d", &length);
queue(length,-1);
average=totalTurns/permutations;
printf("The average turns are %f", average);
}
}

Resources