Error in program to get the max value - c

I am trying to write a program to get the maximum value but it is not working. The calculation is performed inside a function named max_number.
What is the error?
#include <stdio.h>
int max_number(int storeX[], int i)
{
int max=0,x;
for(x=0;x<i;x++)
{
if(storeX[x]<max)
{
max = storeX[x];
}
return max;
}
return 0;
}
int main()
{
int i,x,numbers,max;
printf("how many numbers do you want to compare?\n");
scanf("%d",&i);
int storeX[i];
for(x=0;x<i;x++)
{
printf("the %d number is:",x+1);
scanf("%d",&numbers);
numbers=storeX[x];
}
max=max_number(storeX,i);
printf("the max number is: %d",max);
return 0;
}

There were some problems in the code, have made changes to the code to make it work. Check the comments in the code to understand.
#include <stdio.h>
#include <limits.h>
int max_number(int storeX[], int i)
{
int max=INT_MIN,x; //In case you array has all negative numbers
for(x=0;x<i;x++)
{
if(storeX[x]>max) // This if condition is wrong
{
max = storeX[x];
}
//return max; // You need to iterate the whole array
}
return max;
}
int main()
{
int i,x,numbers,max;
printf("how many numbers do you want to compare?\n");
scanf("%d",&i);
int storeX[i];
for(x=0;x<i;x++)
{
printf("the %d number is:",x+1);
scanf("%d",&storeX[x]); // Need to pass a reference to the array index
//numbers=storeX[x]; //dosen't assign the value to storeX[x] instead the otherway around
}
max=max_number(storeX,i);
printf("the max number is: %d\n",max);
return 0;
}

Related

Perfect numbers representation problem with function

The task is:
Write a program that prints the first n perfect numbers. Checking that the number is perfect should be done in the perfect function.
I did it like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
/*
*/
int perfect_number(long int n)
{
long int sum=0;
for(int j=1;j<n;j++)
{
long int candidate=j;
if(n%candidate==0)
{
sum=sum+candidate;
}
}
if(sum==n)
{
return n;
}
return 0;
}
int main()
{
int n;
printf("Enter how much perfect numbers you want :");
scanf("%d",&n);
while(n<1)
{
printf("Enter how much perfect numbers you want:");
scanf("%d",&n);
}
int counter=0;
for(long int i=1;counter<n;i++)
{
if(perfect_number(i))
{
printf("%ld ",i);
counter++;
}
}
return 0;
}
The problem arises when I type that I want, the first 5 perfect numbers or more. The program will print only 4 and will continue to work, it will search for numbers but will not print anything.
If I type in the first four perfect numbers, they will print 4 and finish the program, they will do everything right.
I thought the problem was in representing the fifth number, so I replaced int with long int, but that didn't help.

Why is the output not being printing?

This is a program to print armstrong numbers in a given range entered by the user, but when this program is executed and inputs are entered the outputs are not displayed.
#include <stdio.h>
int main()
{
int low,high,sum,rem,orig_i;
printf("enter lower and higher numbers : ");
scanf("%d %d",&low,&high);
sum=0;
while(low<high)
{
for(int i=low+1;i<high;i++)
{
while(i!=0)
{
rem=i%10;
orig_i=low+1;
sum=sum+(rem*rem*rem);
if(sum==orig_i)
{
printf("%d",orig_i);
}
i=i/10;
}
}
}
return 0;
}
As you are not increasing low, your loop is running infinitely and a few more errors too.
You can do like this. Although there are a lot of efficient ways to write this programs but i have done a little changes only to make it work.
#include <stdio.h>
int main()
{
int low,high,sum,rem,orig_i;
printf("enter lower and higher numbers : ");
scanf("%d%d",&low,&high);
while(low<high)
{
sum=0;
orig_i = low+1;
for(int i=orig_i;i!=0;i=i/10)
{
rem=i%10;
sum=sum+(rem*rem*rem);
}
if(sum==orig_i)
{
printf("%d\n",orig_i);
}
low++;
}
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.

Largest Among ten numbers using Arrays

What's wrong with my code? It always returns the last number but not the largest? I have spent the last half and hour scratching my head & I still can't find what's wrong here..
Please help
// Largest among ten numbers
#include <stdio.h>
#include <conio.h>
int num[10],large,b; // Global Variables
int largest(int a); // Function protype
int main()
{
for(int i=0;i<5;i++)
{
printf("Enter number %d = ",i+1);
scanf("%d",&num[i]);
large = largest(num[i]); // Calling Function
/*
printf("Num(%d)= %d",i,num[i]); // Testing
printf("\nLargest for now = %d\n\n",large); // Testing
*/
}
printf("\n\n\n%d is the largest",large);
getch();
}
int largest(int a) // Function definition
{
if (a>=b)
{
return a;
b=a;
}
else
{
return b;
}
}
The problem is here:
if (a>=b)
{
return a;
b=a;
}
You return before assigning so b is always 0. Just do the assignment before returning i.e.
if (a>=b)
{
b=a;
return a;
}
Demo

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