division in while loop - c++

I want to use a while loop to divide n numbers and print the quotient. For example if the user entered n=3, the program will ask for 3 numbers. Say the user entered 2, 2, and 3. The program then performs the following operation: 2/2/3 and then prints the answer ( i.e. 0.3333). I have a program that uses a while loop for addition, division, and multiplication perfectly, but in division, it divides 1 by all the numbers entered(1/2/2/3). How do I fix it? Here is the code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main(void)
{
int n, k=0;
float total=1, number;
printf("\nEnter the number of elements you want to divide:");
scanf("%d",&n);
printf("Please enter %d numbers one by one: \n",n);
while(k<n)
{
scanf("%f",&number);
total=number/number;
k=k+1;
}
printf("Quotient of %d numbers = %f \n",n,total);
_getch();
}

Change
total=number/number;
to
if (k==0)
total = number;
else
total=total/number;

Start with k=1 then right before the while loop put
scanf('%f', &total);
You might want to wrap that line in if (n!=0). This way there's no if statement in the loop for very marginal, minuscule performance gains!
Then just change
total=number/number;
to
total=total/number;
and you should be set to go.

Related

Print statement after for loop generating an additional iteration

I am very much a beginner in C++ and was just delving into for loops when I ran into a problem that I solved by winging it and not by understanding.
My script adds numbers from 1 to 10 and calculates the average. The thing is that I had to introduce a new variable "number", other than "sum" and "count", to not have the average be wrong.
#include <iostream>
int main ()
{
float count, sum, avg, number;
sum=avg=number=0.0;
for (count=1.0;count<=10.0;count+=1.0)
{
sum=sum+count;
avg=sum/count;
number=count;
}
printf("\n\nThe number of iterations is %1.f",number);
printf("\nThe sum of the numbers between 1 and 10 is = %.1f",sum);
avg=sum/number;
printf("\nThe average is %.1f",avg);
}
Produces the right result, but
#include <iostream>
int main ()
{
float count, sum, avg;
sum=avg=0.0;
for (count=1.0;count<=10.0;count+=1.0)
{
sum=sum+count;
avg=sum/count;
}
printf("\n\nThe number of iterations is %1.f",count);
printf("\nThe sum of the numbers between 1 and 10 is = %.1f",sum);
avg=sum/count;
printf("\nThe average is %.1f",avg);
}
Gives one more iteration than I expected and causes the average to be wrong. I assume that maybe calling "count" to print adds up but have no certainty and would really like to know why. Sorry if the question but I couldn't figure it out.
In this loop
for (count=1.0;count<=10.0;count+=1.0)
{
sum=sum+count;
avg=sum/count;
number=count;
}
number can not be greater than 10.0.
But in this loop
for (count=1.0;count<=10.0;count+=1.0)
{
sum=sum+count;
avg=sum/count;
}
count is greater than 10.0 after exiting the loop due to the condition count<=10.0.
So these statements
avg=sum/number;
and
avg=sum/count;
produce different results because number and count are unequal after the loops.

TIME complexity of this criss-cross looped program. this is to print N prime numbers

I want to know how to calculate the TIME complexity for this program, the 10 in outer loop defines the number of primes to print. You can suppose it as N. for now this prints 10 prime numbers.
#include <iostream>
using namespace std;
int main()
{ int number=2;
bool flg=0;
cout<<2<<"\t";
number=3;
for(int count=1;count<10;number+=2){
for(int j = 3;j<(number/2);j+=2){
if(number%j==0){
flg=1;
break;
}
}
if(flg==0)
{cout<<number<<"\t";count++;}
else
flg=0;
}
return 0;
}
Assuming integer operations are counted as constant time and ignoring limited range of actual C++ integers:
Because of the prime number theorem, the outer loop will be iterated Theta(N ln(N)) times.
Asymptotically almost all of these iterations are for non-primes because the density of non-primes in the (odd) natural numbers is converging to one.
The inner loop for a non-prime m takes Theta(m) time.
Therefore, in total, the time complexity of the algorithm is Theta(N^2 ln(N)).

Instructions on an assignement on dev c++

So it has been a week since I started using dev c++ and our teacher gave us 2 excercises. I am new to the whole programming thing so I would like some help. The first excercise is about making a program with which you input integers until the sum surpasses 100. Then it should output the total sum and the amount of numbers inputed. The second one says: input 10 integers and show how many times the user has inputed consecutive numbers. For example: -5, 10, 17, -31, -30, -29, 75, 76, 9, -4 the program should show Pair=3 because of : {-31, -30}, {-30, -29} and {75, 76}. I think I have done the first one since the output screen shows exactly what our teacher wants but I am curious if there are any mistakes. But for the second I can't find a way to increase the number of pairs everytime the user inputs 2 consecutive numbers
1)
#include <stdio.h>
int main() {
int x,numbers,sum=0;
do{
printf("Enter number: ");
scanf("%d",&x);
sum+=x;
if(sum<100){
numbers++;
} }
while(sum<100);
printf("Sum: %d\t Numbers: %d",sum,numbers);
return(0);
}
2)
#include <stdio.h>
int main()
{
int i,number,pairs=0;
for(i=1; i<=10; i++){
printf("Enter number: \n");
scanf("%d",&number);}
if(number+=number);
{
pairs++;}
printf("Pairs: %d\n",pairs);
return(0);
}
For (1):
You only increment numbers if the sum is less than 100. So if the first number is greater than 100, you output "Sum: 100 Numbers: 0", even though numbers=1.
For (2):
This simply won't work. Since this is an assignment, I'll just recommend you keep track of three things: the last seen number, the current number, and the number of consecutive pairs seen.

In c++, how can i display all numbers of a linear series?

This is what i have so far. I can display the first, second, and last numbers of the series.
int main()
{
int a,b,c;
cout<<"The first number: "<<endl;
cin>>a;
cout<<"The difference: "<<endl;
cin>>b;
cout<<"How many numbers are in the series: "<<endl;
cin>>c;
cout<<"The numbers of the linear series are: "<<endl;
cout<<a<<endl;
cout<<a+b<<endl; cout<<a+b*(c-1)<<endl;
return 0;
}
I tried the 'for' cycle, but i have the same problem; i can't display c amount of numbers, becouse then i would have to make
cout<<a+b; cout<<a+b*2; cout<<a+b*3;
and so on. And this wouldn't even make sense if the series only had 2 elements.
How can i show the numbers between the first and last numbers of the linear series? Does it have to do something with 'string'? Also, is there a way to do something like
cout<<a*1,a*2,a*3...a*n;
I know that this code doesn't make sense, but hopefully you get the idea
When constructing linear sequences all numbers are of the form f(n) = a + b*n, you can put this to your advantage using a for loop as you mention that you have tried. You should be able to figure it out from the following pseudocode:
Output "The numbers of the linear series are: "
For i = 0 to c-1
Output a + b*i
Next i

How is this code working for finding the number of divisors of a number?

http://www.spoj.com/problems/NDIV/
This is the problem statement. Since i'm new to programming, this particular problem ripped me off, I found this particular code on the internet which when I tried submitting got AC. I want to know how this code worked, as I have submitted it from online source which itself is bad idea for beginners.
#include <bits/stdc++.h>
using namespace std;
int check[32000];
int prime[10000];
void shieve()
{
for(int i=3;i<=180;i+=2)
{
if(!check[i])
{
for(int j=i*i;j<=32000;j+=i)
check[j]=1;
}
}
prime[0] = 2;
int j=1;
for(int i=3;i<=32000;i+=2)
{
if(!check[i]){
prime[j++]=i;
}
}
}
int main()
{
shieve();
int a,b,n,temp,total=1,res=0;
scanf("%d%d%d",&a,&b,&n);
int count=0,i,j,k;
for(i=a;i<=b;i++)
{
temp=i;
total=1;
k=0;
for(j=prime[k];j*j<=temp;j=prime[++k])
{
count=0;
while(temp%j==0)
{
count++;
temp/=j;
}
total *=count+1;
}
if(temp!=1)
total*=2;
if(total==n)
res++;
}
printf("%d\n",res);
return 0;
}
It looks like the code works on the sieve of eratosthenes, but a few things i'm unable to understand.
Why the limit of array "check" is 32000?
Again why the limit for array prime is 10000?
Inside main, whatever is happening inside the for loop of j.
Too many confusions regarding this approach, can someone explain the whole algorithm how it's working.
The hard limit on the arrays is set probably because the problem demands so? If not then just bad code.
Inside the inner loop, you are calculating the largest power of a prime that divides the number. Why? See point 3.
The number of factors of a number n can be calculated as follows:
Let n = (p1)^(n1) * (p2)^(n2) ... where p1, p2 are primes and n1, n2 ... are their exponents. Then the number of factors of n = (n1 + 1)*(n2 + 1)...
Hence the line total *= count + 1 which is basically total = total * (count + 1) (where count is the largest exponent of the prime number that divides the original number) calculates the number of prime factors of the number.
And yes, the code implements sieve of Eratosthenes for storing primes in a table.
(Edit Just saw the problem - you need at least 10^4 boolean values to store the primes (you don't actually need to store the values, just a flag indicating whether the values are prime or not). The condition given is 0 <= b - a <= 10^4 , So start your loop from a to b and check for the bool values stored in the array to know if they are prime or not.)