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
Related
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.
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.
Ok so, I am trying to write a program using a 'while' loop to compute the the average of a certain # of test scores. My 1st input is the amount of tests, and every input afterwards is a set of scores (so say 1st input is 5, then the next 5 inputs are 5 different test scores). I have to input all the variables at once in order to find the sum of all the test scores and then the computed average.
I am completely stuck on how to do this and don't even know where to start.
Some pseudocode
total <- 0
N <- input number of tests
i <- 1
while i <= N
data[i] <- input data
total <- total + data[i]
i <- i + 1
avg <- total / N
To get you started but not give too much away...
To do this the way I think you want to do this:
First take input for the # of tests.
Cin >> test_amt;
Okay, so now you know how many tests there are. Great! So now you should use a while loop to go through each test and take in input for the score! I would use a for loop here, but if you want to use a while loop, then sure.
counter = 0;
while(counter != test_amt-1)
{
cin >> score;
//I would use a vector to store each score.
//this way, you easily know which score matches up with which test
score = 0;
counter++;
}
I hope this can get you started. If not, just let me know and I would be glad to help more.
You can store scores in a std::vector and calculate the sum with std::accumulate.
Take all the scores in a single variable by adding them one after other and finally get the average. Below I have given a hint that will help you to use while loop. But you can use your own logic for it:
int test_amount, counter = 0, total = 0, score;
int avg;
cout<<"Enter test amount"<<endl;
cin>>test_amount;
while(counter < (test_amount-1))
{
cout<<"Enter the test score"<<endl;
cin>>score;
total=total+score;
counter++;
}
avg = total/test_amount;
If you wish to store the scores as well you may think of vector or array. As the number of 'test amount' is not fixed, you can also go for dynamic array, linked list etc. Think about other approaches, post your own code and Google is also there with all kind of help.
Given a list of numbers in increasing order and a certain sum, I'm trying to implement the optimal way of finding the sum. Using the biggest number first
A sample input would be:
3
1
2
5
11
where the first line the number of numbers we are using and the last line is the desired sum
the output would be:
1 x 1
2 x 5
which equals 11
I'm trying to interpret this https://www.classle.net/book/c-program-making-change-using-greedy-method using stdard input
Here is what i got so far
#include <iostream>
using namespace std;
int main()
{
int sol = 0; int array[]; int m[10];
while (!cin.eof())
{
cin >> array[i]; // add inputs to an array
i++;
}
x = array[0]; // number of
for (int i; i < x ; i++) {
while(sol<array[x+1]){
// try to check all multiplications of the largest number until its over the sum
// save the multiplication number into the m[] before it goes over the sum;
//then do the same with the second highest number and check if they can add up to sum
}
cout << m[//multiplication number] << "x" << array[//correct index]
return 0;
}
if(sol!=array[x+1])
{
cout<<endl<<"Not Possible!";
}
}
Finding it hard to find an efficient way of doing this in terms of trying all possible combinations starting with the biggest number? Any suggestions would be greatly helpful, since i know im clearly off
The problem is a variation of the subset sum problem, which is NP-Hard.
An NP-Hard problem is a problem that (among other things) - there is no known polynomial solution for it, thus the greedy approach of "getting the highest first" fails for it.
However, for this NP-Hard problem, there is a pseudo-polynomial solution using dynamic programming. The problem where you can chose each number more then once is called the con change problem.
This page contains explanation and possible solutions for the problem.
#include <iostream>
using namespace std;
int main(){
int a,b,hcf=0,i=1;
cout<<"Enter Value :";
cin>>a;
cout<<"Enter value :";
cin>>b;
while(i<=a || i<=b){
if(a%i ==0 && b%i ==0)hcf=i;
++i;
}
return 0;
}
or Remainder Method ?
Are you finding the hcf at all ? It looks like that you are trying to reverse a number.
Unless the numbers involved are really small, Euclid's algorithm is likely to be a lot faster. This one is linear on the size of the number (with two divisions per iteration, and divisions are one of the slowest types of instruction). Euclid's is actually fairly non-trivial to analyze -- Knuth V2 has several pages on it, but the bottom line is that it's generally quite a bit faster.
If you want to use a variation on the one you're using now, I'd start with i equal to the smaller of the two inputs, and work your way down. This way, the first time you find a common factor, you have your answer so you can break out of the loop.