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.
Related
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.
OJ result :partial accepted
Description
Put n balls into 50 boxes numbered 0, 1, 2, ......, 49, (n<250) and require that the ith box must contain pi times (m to the i-th power) of balls, or none if the box cannot satisfy this condition. Where 2 <= m <= 16 and pi is an integer between 0 and m - 1. Find a specific solution for releasing the balls.
The input file has only one line, just n and m, separated by an empty space.
The output file has several rows, each with two values, first the number of the box and the number of balls in the box, and if there are no balls in the box then no output, but output in ascending order of the box number.
enter image description here
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,m;
cin>>n>>m;
for(int i=0;i<=50;i++){
if(n%m){
long long x=(long long)(pow(m,i));
cout<<i<<" "<<n%m*x<<endl;
}
n=n/m;
}
}
Potential errors in the code include:
The problem statement specifies there are 50 boxes, numbered 0 to 49, but the code loops over 51 boxes, numbered 0 to 50, with for(int i=0;i<=50;i++).
pow should not be used for integer exponentiation because it is inefficient in this use, presents overflow and precision issues, and some low-quality implementations of pow do not return a correct result even when the result is exactly representable. For example, pow(10, 3) might return 999.9999999999998863131622783839702606201171875, after which conversion to long long produces 999.
The long long type may overflow.
i tried to count the number of products which are odd or divisible by 4 , generated by all possible sub-arrays but my implementation get O(n^2).... i need in O(n) time . I also tried to get some pattern but cant found it
here is my code
#include<bits/stdc++.h>
#define lli long long int
using namespace std;
int main()
{
lli testcases,x,M=1000000007;
cin>>testcases;
for(x=0;x<testcases;x++){
lli n,i,j,temp,count1=0;
cin>>n;
vector<lli>v;
for(i=0;i<n;i++){
cin>>temp;
v.push_back(temp);
}
for(i=0;i<n-1;i++){
if(v[i]%2!=0 || v[i]%4==0){
++count1;
}
temp=v[i];
for(j=i+1;j<v.size();j++){
temp*=v[j];
if(temp%2!=0 || temp%4==0){
++count1;
}
}
}
if(v[n-1]%2!=0 || v[n-1]%4==0){
++count1;
}
cout<<count1<<"\n";
count1=0;
}
return 0;
}
thanks in advance !
The question is asking for the number of subarrays whose product is odd (zero factors of two) or a multiple of four (at least two factors of two).
We can also invert this: take the number of subarrays (2**N) and subtract the number of subarrays that have exactly one factor of two.
So, first preprocess the array and replace every number with its factors of two (ie 7 becomes 0, 8 becomes 3, etc).
The question is then "how many subarrays sum to exactly one", which has a known solution.
this question is directly linked to ( april long challenge) from codechef. i don't think its a good idea to ask directly here before the closing of contest (3:00 pm , 13/04/2020).
please obey rules and regulations of codechef. you can check out at this link if you don't believe my words.
https://www.codechef.com/APRIL20B/problems/SQRDSUB or directly visit codechef april challenge (squared subsequence).
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
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.