I can't seem to figure out what is wrong in my code, when It comes to the first scanf in do while loop, I enter a number and it just stops there.
If I put printf("Something"); after that scanf, it isn't printed so it isn't a infinite loop. Also program does not just freeze, when I go into Task Manager, it takes up CPU, so it is doing something.
My program is supposed to load resistors and calculate their parallel equivalent, until I type in 'done', and then print out the calculated number.
#include <stdio.h>
#include <stdlib.h>
typedef struct o{char om[20];}RESISTOR;
int finish(char *s)
{
if( s[0]=='d' && s[1]=='o' && s[2]=='n' && s[3]=='e' && s[4]==0 )
return 0;
else return 1;
}
int power(int n, int pows)
{
int expo=1;
while (pows)
{
expo*=n;
pows--;
}
return expo;
}
int convert(char *s)
{
int broj,c;for(c=-1;s[c];c++);
for(int i=0;s[i];i++)
{broj+=(s[i]-0x30)*power(10,c-i);}
return broj;
}
double paralel(double old, int new)
{
double num;
num=((double)new*old)/(old+(double)new);
return num;
}
int main()
{
int n=0;double para;
RESISTOR *p=(RESISTOR *)malloc(1*sizeof(RESISTOR));
int *convertnum=(int *)malloc(1*sizeof(int));
do
{
printf("R%d= ",n+1);
scanf(" %s", (p+n)->om);
convertnum[n]=convert((*(p+n)).om);
if(n==0) para=convertnum[n];
else if (finish((*(p+n)).om)) para=paralel(para,convertnum[n]);
n++;
if(finish((*(p+n-1)).om))
{
p=(RESISTOR *)realloc(p, n*sizeof(RESISTOR));
convertnum=(int *)realloc(convertnum, n*sizeof(int));
}
}while(finish((*(p+n-1)).om));
printf("\n");printf("\n");
printf("Re= %.2f",para);
free(p);free(convertnum);
printf("\n");
return 0;
}
for(c=-1;s[c];c++); accesses s[-1], which might be 0, which means c-i in power(i,c-i) is negative, and thus the while(pows){pows--} has to go through 2^64 loops which may take a while. Your program should not take any measurable cpu time.
Related
The question is as follows.
Q) Develop a function f1 to accept an integer argument, reverse the digits and return the reversed value. Also, develop another function f2 to accept two integer arguments x and n and to return the value of x raised to the power n. The return value of f2 should be passed on to f1 and the return value of f1 must be checked whether it is prime or not by another function f3. The result must be printed in the program.
The control does not flow beyond the 45th line in this code. I do not really know what it the issue here because when i run the code in code blocks, the output screen displays the output of f2 and then it remains idle. for example, if i give the input as 8 and 2, then the output returns a value of 81 and then it does not do anything beyond that.
#include <stdio.h>
#include <math.h>
int f1(int npow)
{
int ret=0;
while (npow>0)
{
ret=(ret*10)+(npow%10);
}
return ret;
}
int f2(int x,int n)
{
int res;
res=pow(x,n);
return res;
}
int f3(int resrev)
{
int i,check;
for (i=2;i<resrev;i++)
{
if (resrev%i==0)
{
check =1;
break;
}
else
{
check=0;
break;
}
}
return check;
}
void main()
{
int resrev,x,n,npow,prime;
printf("Enter two numbers x and n\n");
scanf("%d %d",&x,&n);
npow=f2(x,n);
printf("x to the power n is %d\n",npow);
resrev=f1(npow);
printf("Reversed value of x to the power n is %d \n",resrev);
prime=f3(resrev);
if (prime==1)
printf("It is not a prime number \n");
else
printf("It is a prime number\n");
}
Problem is in function f1. It is an infinite loop.
Loop condition is until npow>0 but you are not updating value of npow in the loop.
Following is corrected code:
int f1(int npow)
{
int ret=0;
while (npow>0)
{
ret=(ret*10)+(npow%10);
npow = npow/10;
}
return ret;
}
Loop inside f1 is infinite as condition to exit while loop is unaltered.
snippet has to be updated as follows,
int f1(int npow)
{
int ret=0;
while (npow>0)
{
ret=(ret*10)+(npow%10);
npow /= 10; // divide to remove lsb digit
}
return ret;
}
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;
}
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.
I need to write a function to compute a^b but I am not allowed to use pow. Any ideas? I am lost.
It looks like problem is in main now...
Somewhere it gets that vys is what i characterise it. So if i set that vys=1 in main i get 1 in output..
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <unistd.h>
void multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *=b;
i++;
}
return vys;
}
main(void)
{
int b=0, n=0, vys=1;
printf("Give numbers b and n but they must be in interval <0,10>!\n");
scanf("%d %d", &b, &n);
if ((b < 0 || b>10) || (n<0 || n>10))
{
printf("Numbers are not in interval <0,10>!\n");
}
else
{
printf("Number is in interval so i continue...\n");
sleep(2);
vys= multiplied(&b, &n);
printf("%d", vys);
}
Let's be explicit.
First, this
void multiplied(int *b, int *n)
returns an int, so say so.
int multiplied(int *b, int *n)
Next, you initialised variables in main: do the same here.
int i, vys;
Like this:
int i=1, vys=1;
Now let's look at the loop:
while (i<=n)
{
vys=*b**b;
i++;
}
As it stands, you are setting vys to something over and over again in the loop.
You want to multiply up, e.g. 2, then 2*2, then 2*2*2, .... if you want a power of two:
while (i<=n)
{
vys *= *b;
i++;
}
Now, you don't need to pass pointers.
int multiplied(int b, int n)
{
int i=1, vys=1;
while (i<=n)
{
vys *= b;
i++;
}
return vys;
}
Edit:
Watch out for when you call the function:
main(void)
{
int b=0, n=0, vys;
//input and checking code as you have it
multiplied(&b, &n); //<---- return ignored
printf("%d", vys); //<-- print uninitialsed local variable
}
Change you last two lines:
vys = multiplied(&b, &n); //<---- return captured
printf("%d", vys); //<-- print returned variable
Edit 2:
With the change to use int in the function and not pointers, pass the ints not their addresses:
vys = multiplied(b, n); //<---- pass the ints not their addresses
printf("%d", vys); //<-- print returned variable, which should vary now
Here you have a simple code:
#include <stdio.h>
long long intpow(int a, int b)
{
long long tempres = 1;
while(b--)
tempres *= a;
return tempres;
}
int main(void) {
printf("%lld\n", intpow(5,10));
return 0;
}
you need much larger int to accommodate the result.
You can play with it yourself: https://ideone.com/4JT6NQ
This is one of my first C programs. I am trying to make a guess the number game and I have used the debugger to work out that the line where I am calling for the rand_number function the variable 'r' is going back to '1'. I can't work out why.
If I put the code into the main() the program works as expected but I am wanting to use a function for experience
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void rand_number (int r) //Function to generate a random number and assign it to 'r'.
{
srand(time (NULL));
r = rand()%1000+1;
}
int main()
{
int guess, r, correct=0;
rand_number (r); //Run the random number function (This is where 'r' is switching back to '1'
printf("Guess a number!\n"); //User takes a guess
scanf("%d", &guess);
while (correct != 1) //Loop to give the user multiple guesses until they are correct
{
if(guess == r)
{
correct++;
}
else if (guess < r)
{
printf("Too low, please guess again.\n");
scanf("%d", &guess);
}
else
{
printf("Too high, please guess again.\n");
scanf("%d", &guess);
}
}
printf("Congratulations! You guessed it!");
return 0;
}
You are passing r to rand_number() by value, that means rand_number() can only modify it on it's local scope.
If you want it to modify the original variable in the caller you have to pass it as a pointer.
void rand_number(int *r)
{
*r = rand()%1000;
}
int main()
{
int r;
rand_number(&r);
...
}
Or simply return the value.
int rand_number()
{
return rand()%1000;
}
int main()
{
int r = rand_number();
....
}
Another problem is that you are calling srand() every time. You are supposed to seed it only once, if you keep calling this function you will reset the random number generator and it will keep generating the same number.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int rand_number ()
{
srand(time (NULL));
return rand()%1000+1;
}
int main()
{
int guess, r, correct=0;
r = rand_number (); //return the value here
//rest of the code
}
use the return method to retrieve the value from rand_number
In C, if you want to change a value of a variable inside a function, you have to pass a pointer to the function like this:
void rand_number(int* r)
{
srand(time(NULL));
*r = rand()%1000 + 1;
}
and call it byrand_number(&r). Another way is to return the random number inside the function and assign it to r, like this:
int rand_number()
{
srand(time(NULL));
return rand()%1000 + 1;
}