Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I just start to learn programming and here is a problem about fibonacci sequence:
It is asked to make a function with parameter k(means k-order of fibonacci) and parameter n(means the n th member of sequence) and use that function to get the m th member of a k-order sequence.
f0 = 0, f1 =0, ....fk-2 =0, fk-1=1;
when n=k, k+1,...
fn= fn-1+fn-2+....+fn-k; n=k,k+1,....
(letters and numbers on right-side of f are subscripts which mean the n th member, n-1 th member..)
following is my code:
int Fibonacci(int k, int n){
int result=0;
if (n==k-1) return 1;
if (n<k-1) return 0;
if (n>=k){
for(int i=n-1; i>i-k-1;--i){
result+=Fibonacci(k,i);
cout<<result<<endl;
}
return result;
}
}
new version
int result=0;
int Fibonacci(int k, int n){
if (n==k-1) return 1;
if (n<k-1) return 0;
if (n>=k){
for(int i=n-1; i>n-k-1;--i){
result+=Fibonacci(k,i);
cout<<result<<endl;
}
return result;
}
}
my question is why this code cannot come out correct answer? there should be some problems in the loop but I cannot find them. Will someone help me?
for(int i=n-1; i>i-k-1;--i)
i will pretty much always be greater than i-k-1
Maybe you mean for(int i=n-1; i>n-k-1;--i), but you should check.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
Side note: I've been trying to trace this code, but every time I end up with output, it seems
to be far more or less than the right answer which is 30. If anybody can explain in to me, I'd be more than appreciated. Thanks
Always make sure to add { and } to loops so that you avoid thinking that some instruction is within a loop when it's actually not.
The code you added in the screenshot is equivalent to the following
#include <iostream>
using namespace std;
int main() {
int t = 0;
for (int c = 0; c < 3; ++c) {
// +8
for (int v = 0; v < 4; ++v) {
for (int h = 0; h < 2; ++h) {
++t;
}
}
// +2
++t;
++t;
}
cout << t << endl;
return 0;
}
So, each iteration of the first loop (variable c) adds 10 to t. There are three iterations of the first loop, the total value added to t is 30.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hi everyone!!! I was writing contest and actually this is not difficult question but I stuck on it. Please help, here my code that passed only three tests and failed on fourth test. Answer must not use function and pointers. Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
long long int max=a[0],min=[0],ind1=0,ind2=0;
for(int i=0;i<n;i++){
if(a[i]>=max) {
max=a[i];
ind2=i;
}
if(a[i]<=min) {
min=a[i];
ind1=i;
}
}
int sum=0;
if(ind1<ind2)
for(int i=ind1+1;i<ind2;i++){
if(a[i]%2==0) sum++;
}
else if(ind1>ind2)
for(int i=ind2+1;i<ind1;i++){
if(a[i]%2==0) sum++;
}
cout<<sum;
}
It looks like you're trying to count the number of even elements between the min and max element in the array?
It may have failed the last test because you set initial min/max to garbage. In the case where a[0] is initialised to zero, and all the elements in the array are negative, the reported max will be incorrect, for example. You need to set max to the minimum integer and vice versa.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Given an array A (may contain duplicates) of N elements and a positive integer K. The task is to count the number of elements which occurs exactly floor(N/K) times in the array.
i tried to compare the starting element of array with next elements in the array and increment count if they are equal and if count is equal to floor(N/K) then increment the total number of elements which repeat exactly floor(N/K) times . But i don't get the correct the output. can you please correct me where i'm wrong with my logic.
int countSpecials(int arr[], int sizeof_array, int K){
int f = floor(sizeof_array/K), count = 1,p=0;
// Your code here
for (int i=0;i<sizeof_array;i++){
if (arr[i]>0)
{
for(int j=i+1;j<sizeof_array;j++)
{
if(arr[i]==arr[j])
count++;
}
if (count==f)
{
p++;
}
}
}
return p;
}
Use a std::map to count the number of occurances of each array element, then iterate the map checking which items have the count value you are looking for.
#include <map>
#include <algorithm>
int countSpecials(int arr[], int sizeof_array, int K)
{
int f = floor(sizeof_array/K);
std::map<int, int> m;
for (int i = 0; i < sizeof_array; i++)
m[arr[i]]++;
return std::count_if(m.begin(), m.end(),
[=](const std::map<int, int>::value_type &elem){ return (elem.second == f); }
);
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int sumTo(int value)
{
int total(0);
for (int count=1; count <= value; ++count)
total += count;
return total;
}
Where do I add cout<<total; to print the results? I have tried to add the print function several locations without success -- either getting nothing or a large number (maybe a number previously stored in the location). Have debugger with the result showing total as 15 when value is equal to 5.
In addition, I have added a void function to print, but still no correct results.
Need help with the proper placement of the print function to yield the correct answer?
This should work :
#include <iostream>
int sumTo(int value)
{
int total = 0;
for (int count=1; count <= value; ++count)
total += count;
return total;
}
int main(void)
{
std::cout << sumTo(12);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
My code is below but there is some error can anyone guide me to write logic to return continuous numbers, for example, if array[] = {1,3,5,2,3,4,7,4,5,6} then function should return 2,3,4,4,5,6 keep time complexity in mind?
#include <stdio.h>
#define max 10
int coll[max];
void call_sort(int* p) {
int i = 0, first, sec;
while (*p) {
first = *p;
p++;
sec = *p - 1;
if (first == sec) {
coll[i] = *p;
i++;
}
int j;
for (j = 0; j < max; j++) {
printf("%d ", coll[j]);
//coll=coll+1;
}
}
}
int main(void) {
printf("ya\n");
int buff[max], i;
for (i = 0; i < max; i++)
scanf("%d", buff[i]);
call_sort(&buff);
}
There are many problems with your code
Wrong parameter to scanf(), you need to pass the address of the variable to modify it inside scanf() like this
scanf("%d", &buff[i]);
and you should also check that scanf() did read the value correctly by checking it's return value.
Wrong parameter to call_sort(), this will not cause any problem in this case, but it's wrong, and this combined with the scanf() issue, means that you did not enable compiler warnings, you should.
The correct way to pass buff is simply
call_sort(buff);
Wrong while (*p) which assumes that 0 is the last element of the array.
You should probably pass the size as a parameter and write a for loop, since you are dealing with numbers and 0 is a number, if it were a text string then it would be ok to exclude 0 from the normal values and use it as a sentinel value which is done in the standard library functions for strings.