Click here to Skip to main content
14,360,787 members
Rate this:
Please Sign up or sign in to vote.
See more:
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int T,i,j,k,l;
    cin>>T;                           //no of test cases

    long int P[T];

    for(i=0;i<T;++i)
    {
        long int A[T],N[T],E,D[T],size,F[T],Z,m;

        cin>>N[i];
		
        A[0]=0;
        A[1]=1;

        for(m=2;m<N[i];++m)      //fibonacci series of N terms
        {
            A[m]=A[m-1]+A[m-2];

            A[m-2]=A[m-1];
            A[m-1]=A[m];
        }

        for(m=0;m<N[i];++m)
        {                         //finding remainder of fibonacci series  
            D[m]=A[m]%10;
        }

        while(1)
        {
            for(j=0;j<N[i];j+2)
            {
                if(N[i]%2==0)      //to check whether N is even or not 
                {
                    for(k=0;k<(N[i]/2);++k)
                    {
                        F[k]=D[j];     //even elements  copied new array
                        D[k]=F[k];     // updating original array 
                    }

                    N[i]=k;         //updating value of N to last index of new array 
                }
                else
                {
                    if( N[i] == 1 ) // N[i] become 1 breaking it out of loop 
                    {
                        break;
                    }
                    else               // when N is not 1 and is odd 
                    {
                        for(k=0;k<((N[i]+1)/2);++k)       
                        {                 // for odd value half value is N+1/2
                            F[k]=D[j];  

                            //even elements are copied here in new array

                            D[k]=F[k];       

                            // updating original array to only even indices array 
                        }

                        N[i]=k;       //updating  N to last index of new array 
                    }
                }
            }

            if( N[i] == 1 )
            {
                 break;
            }
        }

        P[i]=F[0];            //assigning value to P
    }

    for(i=0;i<T;++i)
    {
        cout<<P[i]<<"\n";
    }
    return 0;
}


What I have tried:

i have run it on different compilers but the result is same the else loop is not being executed the else statement for updating the values for odd value of N is not being executed.
Posted
Updated 7-Oct-19 23:39pm
v4
Comments
Patrice T 7-Oct-19 18:00pm
   
You should tell what is supposed to do the code.
the link to original problem would be nice too.
[no name] 7-Oct-19 23:29pm
   
i have posted original problem link .
Rick York 7-Oct-19 18:41pm
   
I am surprised this even compiles because of how the variable T is used.
[no name] 7-Oct-19 23:29pm
   
why sir ? is the syntax in correct?
Rick York 8-Oct-19 11:23am
   
In the past, array sizes required a constant expression to set their size. As Mr. 0x1AA noted, it seems that is no longer a requirement.
0x01AA 8-Oct-19 4:50am
   
Looks like C++14 does finally support it (G++ supported this since a longer time)
Rate this:
Please Sign up or sign in to vote.

Solution 1

It is not clear what your code is expected to do and where is the (supposed) failing line.
Anyway, the following statement
for(j=0;j<N[i];j+2)
is probably wrong, because
j+2
is useless (you probably meant j+=2).

Turn on ALL compiler warnings and do read the issued ones.

[update]
You should fix your code but, first and foremost, you should really think about the problem. For instance:
You don't really need to compute the Fibonacci series, computing its reminders is just enough.
Possibly the 'odd items elimination' operation is not necessary (try with paper and pencil).
[/update]

[update 2]
Focus yourself on the Dl series. Since it contains only the reminders of the sum of previous items, it must eventually repeat itself. This observation is the key for answering the question with the original constraints (no TLE).
[/update 2]
   
v3
Comments
[no name] 7-Oct-19 23:34pm
   
my code is skipping first else statement and whole body of else statement. here is question link https://www.codechef.com/submit/FIBEASY .
CPallini 8-Oct-19 4:00am
   
See my updated solution.
CPallini 8-Oct-19 17:46pm
   
Please see the Update 2 to my solution.
Rate this:
Please Sign up or sign in to vote.

Solution 2

In a Fibonacci series, your code is too complicated:
A[0]=0;
A[1]=1;
for(m=2;m<N[i];++m)                     //fibonacci series of N terms
{
    A[m]=A[m-1]+A[m-2];

    A[m-2]=A[m-1]; // This line is suspect
    A[m-1]=A[m];   // this line too
}


In order to see what your code is doing exactly, you should learn to use the debugger, it is a great learning tool.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include<iostream>
#include<vector>

using namespace std;

int main()
{
    int T,i,j,k,l;
    cin>>T;                               //no of test cases

    long int P[T];


    for(i=0;i<T;++i)
    {

        long int A[T],N[T],E,D[T],size,F[T],Z,m;
        cin>>N[i];

        A[0]=0;
        A[1]=1;
        for(m=2;m<N[i];++m)                     //fibonacci series of N terms
        {
            A[m]=A[m-1]+A[m-2];

            A[m-2]=A[m-1];
            A[m-1]=A[m];
        }

        for(m=0;m<N[i];++m)
        {                             //finding remainder of fibonacci series
            D[m]=A[m]%10;
        }

        while(1)
        {
            for(j=0;j<N[i];j+2)
            {
                if(N[i]%2==0)          //to check whether N is even or not
                {
                    for(k=0;k<(N[i]/2);++k)
                    {
                        F[k]=D[j];        //even elements  copied new array
                        D[k]=F[k];              // updating original array

                    }

                    N[i]=k;        //updating value of N to last index of new array
                }
                else
                {
                    if(N[i]==1)
                    // N[i] become 1 breaking it out of loop
                    {
                        break;
                    }
                    else                 // when N is not 1 and is odd
                    {
                        for(k=0;k<((N[i]+1)/2);++k)
                        // for odd value half value is N+1/2
                        {
                            F[k]=D[j];

                            //even elements are copied here in new array

                            D[k]=F[k];

                            // updating original array to only even indices array

                        }

                        N[i]=k;
                        //updating  N to last index of new array
                    }
                }
            }
            if(N[i]==1)
            {
                break;
            }
        }

        P[i]=F[0];            //assigning value to P
    }
    for(i=0;i<T;++i)
    {
        cout<<P[i]<<"\n";
    }
    return 0;

}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

[Update]
Quote:
this else statement is not being executed i don't know why?

Because your code is buggy, the else condition is never met.
You need to check that your code is correct at every step, it is debugging.
   
v2
Comments
[no name] 7-Oct-19 23:36pm
   
else // when N is not 1 and is odd
{
for(k=0;k<((N[i]+1)/2);++k)

// for odd value half value is N+1/2
{
F[k]=D[j];

//even index elements are copied here in new array

D[k]=F[k];

// updating original array to only even indices array

}

N[i]=k;
//updating N to last index of new array
}

this else statement is not being executed i don't know why?
Rate this:
Please Sign up or sign in to vote.

Solution 3

How many times do we have to tell you to start using the debugger to work out what is going on in your apps?
I've lost count, because most of your questions are missing from your question list, I wonder why ...

If you can't use the debugger properly, there is little or no point in you continuing to try these exercises: you write code, it doesn't work, you ask here. That's not development, that's "throw it together and hope it works" - which is a very poor methodology and teaches you very little.

Grab the debugger, and start learning to use it: it will improve the quality of your code (currently "poor student" grade) and get you through these tasks faster.
   
Comments
[no name] 8-Oct-19 14:57pm
   
ok sir

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100