csurfer 422 Posting Pro

Check your code again. The looping construct you have used does simple insertion which you want and doesn't sort the vector in any way.try your code on an integer vector may be to confirm the problem.

csurfer 422 Posting Pro

***Your previous thread for the same question****
Why did you create a new thread for the rectification in the question you posted...?
You could have did the correction in the previous thread itself. Moreover it has been answered already in the previous thread.Now its your work to put in a bit of elbow grease to find out the algorithm .

csurfer 422 Posting Pro

@alwaysLearning0 : What you said is almost right but there is nothing called static memory in the context we are talking.

every process is arranged into following segments :
Text Segment : Which holds the code.
Data Segment : Which holds the static and global variables.
The remaining memory allocated to the process is shared by Stack and Heap which grow from opposite ends.

So as said earlier the global and static variables which can be accessed throughout the program is stored in +data segment. And all the variables that we declare in any function (including main) which are non static are given memory space int the respective functions stack which is formed when the function is called and destroyed when the function returns.

csurfer 422 Posting Pro

This is definitely not an exam question its a question picked up from some coding competition or as the "Note:" section says that its an assignment as it speaks of the submission and the criteria to be met before the submission.

Anyways these type of question normally follow a format as to number of test cases followed by important data of the question(here N and K) and then the detailed data(Here the blood group).As :

2
4 2
1 1 4 7
6 3
1 4 8 12 16 20

Here it means that it has two test cases as the first line signifies TC's.
Then the test cases follow where first line of TC has N K format then the N dragons blood group in the next line.So on and so forth for all the other test cases to follow. Now you know how to take inputs come up with your algorithm to solve it :)

csurfer 422 Posting Pro

This is definitely not an exam question its a question picked up from some coding competition or as the "Note:" section says that its an assignment as it speaks of the submission and the criteria to be met before the submission.

Anyways these type of question normally follow a format as to number of test cases followed by important data of the question(here N and K) and then the detailed data(Here the blood group).As :

2
4 2
1 1 4 7
6 3
1 4 8 12 16 20

Here it means that it has two test cases as the first line signifies TC's.
Then the test cases follow where first line of TC has N K format then the N dragons blood group in the next line.So on and so forth for all the other test cases to follow. Now you know how to take inputs come up with your algorithm to solve it :)

csurfer 422 Posting Pro

@Unimportant : hours(minutes/60) would act like a ground function and give 4 hours for 241 minutes and not five.(you need to increase minutes by 60 or add 1 to the hours value in order to make it act like a ceiling function i,e (241+60)/60 = 5 and (241/60)+1 = 5).

@nickx522:You have almost got it you can work around like this:

//Only a psuedocode
if(min <= 120)
   // print it as free parking
else
{
    //Increase minutes by 60 so that when you divide it by 60 you get the effect of ceiling function and not of a ground function
    min+=60;
    int hours(min/60);
    //Now you have the hours for which you need to charge 
}
csurfer 422 Posting Pro

@AncientDragon : Sorry your message was not uploaded when i started writing the post. :)

csurfer 422 Posting Pro

cout always looks out for a type which it can output or another object of its own data type that is "ostream&" but you are passing a void type to it by using the myObject.displayObject() in line with cout.Rectify it and you are done.

csurfer 422 Posting Pro

I think you need to learn the proper syntax of coding first. There is nothing in C++ which is used like this..

if (i = 3; i <=num; i++)
csurfer 422 Posting Pro

The algorithm just says :

//Input Array[0..n-1] unsorted
//Output Array[0..n-1] sorted

for i ranging from 0 to n-2
    min = i;
    for j ranging from i+1 to n-1
        if Arr[j] < Arr[min]
             min = j;
    Swap( Arr[i] , Arr[min] )

You can implement the above algorithm of selection sort in any way you want using variables or pointers .

csurfer 422 Posting Pro

@stevanity : A word of advice , give the algorithm if you want but not the entire code as you did in the post above. Due to this someone may just copy it out and never learn the way you learnt. To a student learning is more important than getting the job done :)

csurfer 422 Posting Pro

In a 3X3 matrix from the centre the flow is
1L,1U,2R,2D,2L
for 5X5 it is
1L,1U,2R,2D,3L,3U,4R,4D,4L

But in general it is always of the form:

1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,........
L,U,R,D,L,U,R,D,............................
where numbers represent the times you need to move and L=left,U=Up,R=Right,D=Down
moving left is decrementing j right is incrementing j
moving up is decrementing i down is incrementing i

Just make the computer do this... and you are done.

csurfer 422 Posting Pro

State your question properly . And show us some code that you have tried to write...

csurfer 422 Posting Pro

@Stevanity :
Nothing comes on a platter. do you really think you had to look at others codes for the above code snippet you have posted ? You could spend some time how you want to move in the array and yourself write the code.
Now after having the above code you want to know how to traverse the opposite way the above code is traversing(i,e from center to outside).

Logically something must be getting interchanged in the above code(if its correct) right ? Can't you put in some elbow grease in working it out ?

csurfer 422 Posting Pro

@andy124 :
So this turns out to be a good opportunity for you to learn C++. Try to implement thats the only way you will learn.

csurfer 422 Posting Pro

If you have inp_Sec as your input of seconds then you can use it as :

//Here you are trying to acquire all those seconds which cannot be converted to minutes ie anything short of sixty seconds
out_Sec = inp_Sec % 60;

//Here you convert it into proper minutes leaving out the extra seconds as it is integer division
inp_Sec = inp_Sec / 60;

//Here you are trying to acquire all those minutes which cannot be converted to hours ie anything short of sixty minutes
out_Min = inp_Sec % 60;

//Here you are trying to acquire all those minutes which can be converted to hours
out_Hours = inp_Sec / 60;
csurfer 422 Posting Pro

Its quite straight forward right ?
Assuming int n = strlen( <Message> );

Then the order of the matrix say ord should be the smallest integer i which satisfies the equation

i*i*2 >= n ( where i is the smallest integer which satisfies this equation )

Now for any square matrix of odd order you can device an algorithm by which you can traverse the matrix in the cyclic order from middle with the matrix being implemented as an arr[2] if you want.

csurfer 422 Posting Pro

If you can make a queue of chars so can you make a queue of strings and in a string you can store the characters which form the entire number. There can be many ways to solve a problem (efficient and in efficient). you just need to choose how you would want to solve it.

csurfer 422 Posting Pro

*+abc should be treated as (a+b)*c so in a prefix expression traverse the expression until you find two operands after an operator and use that operator on that operand then go on doing the same thing for the rest of expression. If you know queue then you can obviously play around. You can even use STL for the ease of programming.

csurfer 422 Posting Pro

@AncientDragon and Rahul.menon

What Ancient Dragon has suggested and what you are trying to do is both right but I have a suggestion here which may sound a bit off the line.
Why not read the floating value as a string(char array) instead of a float ? Because as we know floating point variables are never stored internally as they look on screen which makes their comparision literally impossible without some suffixes to the numbers such as F.
So if your whole plan is to read a floating point value and add its integral and fractional part's digits and print it back then better read it as a string and play around until you have a better purpose to serve.

csurfer 422 Posting Pro

Ok and the code OP showed should be similarly proved right? Like so :

Summation from i = 1 to N
Summation from j = x, where x = 2^i

Thus N * Summation from j = x, where x = 2^i = {1,2,4,8,16...}

If you look on wiki_summation. In the growth rate section it tells you that: Summation from i to N of c^i = THETA(c^n).
In our case c = 2, thus Summation from i to N of 2^i = THETA(2^n);

Thus N * Summation from j = x, where x = 2^i
= N * THETA(2^n)
= THETA(n*2^n)

@firstPerson:

All the rules I have used are under section "Some summations of polynomial expressions" of your given article. And even after checking with the document you sent I am finding that I am on the right path and came up with right complexity for BigOh.But ya if its Theta (average) it may change. I do agree. But normally complexity analysis is done to set the upper limit so I have concentrated on BigOh notation only. Do check your calculations once.Thank You.

csurfer 422 Posting Pro

Problem 1:

Legend :
Summation where z ranges from low to high( ) :: S-z-[low]-[high]( )

Above stated problem is as follows...

S-i-[0]-[n-1]( S-j-[0]-[i-1]( S-k-[0]-[j-1]( 1 ) ) )

S-i-[0]-[n-1]( S-j-[0]-[i-1]( j ) )

S-i-[0]-[n-1]( (i-1)*i/2 )

S-i-[0]-[n-1]( (i^2-i)/2 )

1/2 * { S-i-[0]-[n-1]( i^2 ) - S-i-[0]-[n-1]( i ) }

1/2 * { (n-1)(n)(2n-1)/6 - (n-1)(n)/2 }

On solving...

(n)(n-1)(2n-4)/12

Neglecting the co-efficients its O(n^3).
Or can be said to be of cubic complexity.

Problem 2:

==> 1+2+3+4+5+....+n
==> Pairing the 1st elements form both end 2nd elements form both end and so on...
==> (n + 1)+(n-1 + 2)+(n-2 + 3)+.....
==> (n + 1)+(n + 1)+..... n/2 times
==> (n+1)n/2

Assuming it to be true for n as n(n+1)/2
Summation upto n+1 should be n+1 added to this sum ie,

n(n+1)/2 + n+1
Solving
(n+1)(n+2)/2
Which is actually (n+1)((n+1)+1)/2
hence it proves that the formula is applicable for any n.

csurfer 422 Posting Pro

Just to see what you are thinking, can you explain how you got that in terms of summation, as you did in your proof couple of post ago. I just want to see exactly where we differ and see if I am wrong or if you are wrong.

@firstPerson : Sure here it is..

The code is actually interpreted as..

==> Summation where i ranges from 0 to N-1( Summation where j ranges from 0 to N-1( 1 ) )

As Summation from lowerlimit to upperlimit of 1 is upperlimit - lowerlimit + 1 it goes as

==> Summation where i ranges from 0 to N-1( N-1-0+1 )
==> Summation where i ranges from 0 to N-1( N )
==> N * Summation where i ranges from 0 to N-1( 1 )
==> N * ( N-1-0+1 )
==> N * N
==> BigOh( N^2 )

csurfer 422 Posting Pro

Post the code area in which you are finding a problem.

csurfer 422 Posting Pro

Instead of reading the entire line at a stretch and then trying to cut it convert it etc try reading column by column with space as the delimiter as

//istream& getline ( istream& is, string& str, char delim );
getline(fPtr,str,' ');

If you know exactly how the file is stored then can always read it effectively but developing a generic program may be difficult.You can refer to getline for more details of getline.

csurfer 422 Posting Pro

All that I can see is that in an odd numbered line its adding 1 and in even numbered line the multiples of that even number are printed...
Any more inputs ...???

csurfer 422 Posting Pro

@myk45:

Just explaining what gerard4143 said in simple words.

First usage is allowed as what ever usage of pointer you make its still remains a pointer and takes up a standard amount of memory which is normally equal to the size of your integer variable.So it can be defined without any confusions.

In the second usage you are including a variable of the same structure type inside the structure. Now here in order to declare a variable of some data type you need to know how that data type is defined and the data type cannot be unambigiously defined until the compiler knows what all the variables within that structure.So one waits for other and no one gets completed.

But this is allowed

struct A
{
     int a;
     char b;
};
struct B
{
     int c;
     struct A varA;
};

As the definition of A knows how char and int are defined and their memory needs and before structure B is defined the compiler knows how structure A is defined.

csurfer 422 Posting Pro

Is there a question coming up?

csurfer 422 Posting Pro

@CSurfer
What is the runtime of this :

for i = 0 to N - 1
 for j = 0 to N - 1

@firstPerson:

Its BigOh(N^2)

csurfer 422 Posting Pro

>>Summation of 2^i + 1 from i = 0 to n-2.

That shows right there that its O(n*2^n);

@first person :

==> Summation where i ranges from lowerlimit to upperlimit( 1 )
==> (upperlimit - lowerlimit + 1)

Therefore its

==> Summation of 2^i where i ranges from 0 to n-2

and not Summation of 2^i+1 where i ranges from 0 to n-2.

Therefore final complexity is 2^(n-1) - 1
which can be stated as BigOh(2^n) and not BigOh(n*2^n) as you said.

Please recheck and correct me if I am wrong.

csurfer 422 Posting Pro

You are in the right track.But try to see the code in this way for ease of visualization...

for(int i = 0; i < n-1; i++)
{
   x=(int)pow(2,i);
   for(int j = 1; j <= x; j++)
       cout << j << endl;
}

You can see that here too x and i vary as x = 2 power i just like in your code.

So as you can see the complexity turns out to be

==> Summation where i ranges from 0 to n-2( Summation where j ranges from 1 to 2 power i( 1 ))
==> Summation where i ranges from 0 to n-2( 2 power i )
==> (2 power 0) + (2 power 1) + (2 power 2) + (2 power 3) + ...... + (2 power (n-2))

And you can see that (2 power 0) + (2 power 1) = (2 power 2) - 1;
(2 power 0) + (2 power 1) + (2 power 2) = (2 power 3) - 1 and so on ... Therefore above problem comes to

==> (2 power (n-1)) - 1

And thats your complexity or Big-Oh.Hope you understand...Cannot put it in any better way :)

csurfer 422 Posting Pro

Few things you can try out :
1)Read the blurred out print of the image you so nicely posted(If you can read).
2)Understand the problem and try to solve it.(If you have time)
3)Refer to some good links on the net to get more knowledge(On C++)rather than chickening out.

After you have done these do let us know...

csurfer 422 Posting Pro

getline marches forward in the file once it has read the line,and you have made no effort in "case 1" to read the file from starting again. There lies your problem.

In order to check ,
first try to find account number 1 then 2 then 3 it does give the result.

Now its up to you to fix it. :) Happy coding...

csurfer 422 Posting Pro

I did try with kfree(sock_buff) and also kfree_skb(sock_buff) both are resulting in system hang...In the above program all I have changed is to add the skb_free thing to the end after transmission of the buffer by the xmit() call...

csurfer 422 Posting Pro

I rectified all the mistakes which you had mentioned... and also took care about the kfree of allocated modules... But now its not only tainting the kernel but also is causing immediate hang of the system...

csurfer 422 Posting Pro

@micjan : Congrats you got the code !!! I certainly wish the two smart people above get you everything you want in your life ... because with this attitude you are not gonna learn or earn anything...!!!

Salem commented: I like it! +19
csurfer 422 Posting Pro

@nezachem : The skb_copy returns the correct value... I have checked it... and as far as kfree_skb of sock buff is concerned ... I m not allocating it any memory by some calls... and hence I don't need to de-allocate the memory it has... Thanks

@others : Any suggestions or links in this aspect would be great...

csurfer 422 Posting Pro

Hello Everyone,
I am developing a kernel module which acts as a sniffer and also a module which edits the TCP packet window size as per requirement of the admin. And in the process I developed this kernel module which is tainting my kernel (Opensuse11.2).The sniffing module developed by me works fine but this editing module is dumping the kernel...

#include <linux/ip.h>             
#include <linux/kernel.h> 
#include <linux/module.h> 
#include <linux/netdevice.h>      
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/skbuff.h>         
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/string.h>
#include <linux/ipv6.h> 
#include<linux/inet.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("csurfer");
MODULE_DESCRIPTION("Packet Sniffer");

static struct nf_hook_ops netfilter_ops_out; /* NF_IP_POST_ROUTING */
struct tcphdr *tcp_header;
struct sk_buff *sock_buff;
struct iphdr *ip_header;
int dadd,sadd,bit1,bit2,bit3,bit4;
struct net_device * dev;

/* Function prototype in <linux/netfilter> */
unsigned int main_hook(unsigned int hooknum,  
                  struct sk_buff *skb,
                  const struct net_device *in,
                  const struct net_device *out,
                  int (*okfn)(struct sk_buff*))
{
sock_buff=skb_copy(skb,GFP_ATOMIC);

ip_header=(struct iphdr*)(sock_buff->network_header);

dadd= ip_header->daddr;
sadd=ip_header->saddr;
bit1=255 & sadd;
bit2=(0xff00 & sadd)>>8;
bit3=(0xff0000 & sadd)>>16;
bit4=(0xff000000 & sadd)>>24;

printk("Source IP %d.%d.%d.%d",bit1,bit2,bit3,bit4);

bit1=255 & dadd;
bit2=(0xff00 & dadd)>>8;
bit3=(0xff0000 & dadd)>>16;
bit4=(0xff000000 & dadd)>>24;

printk("     Destination IP %d.%d.%d.%d",bit1,bit2,bit3,bit4);

tcp_header=tcp_hdr(sock_buff);
printk("HW %d",ntohs(tcp_header->window));
tcp_header->window=htons(777);

// Choose the device through which the packet is to be sent
for_each_netdev(&init_net,dev)  
                if(strcmp(dev->name,"eth0")==0)
                        sock_buff->dev=dev;

dev_queue_xmit(sock_buff);             // Transmit the packet
return NF_STOLEN;
}

int init_module()
{
        netfilter_ops_out.hook=main_hook;
        netfilter_ops_out.pf=PF_INET;
        netfilter_ops_out.hooknum=NF_INET_POST_ROUTING;
        netfilter_ops_out.priority=NF_IP_PRI_FIRST;
        nf_register_hook(&netfilter_ops_out);
 /* register NF_IP_POST_ROUTING hook */
	return 0;
}

void cleanup()
{
	nf_unregister_hook(&netfilter_ops_out); 
       /*unregister NF_IP_POST_ROUTING hook*/
}

Can anyone tell me where i m wrong and any suggestions on how I can take it forward...???

csurfer 422 Posting Pro

Before worrying about big things please pay some attention towards small things...
There is a lot of difference between 1 and '1' .
The input you are taking is a character so the cases of switch should be as case '1' : case '2' : and all , else make the input to be taken to a integer rather than a character.

And ya usage of fflush(stdin) is bad. One more point of concern is all the inputs you are taking doesn't actually need a space and hence you don't even need to use getline().You can directly use cin >> temp1; 4 posts without even concentrating on the main problem.This way you will add all the statements in this world but still wont be able to take the inputs.

amrith92 commented: Well Spotted! Man am I blind!! +2
csurfer 422 Posting Pro

OK here it goes.The piece of code provided by you works on the character buffer from which "cin" reads from, rather than the actual number 42.

Take the example of input of number 10.
It is treated as '1' '0' '\n' ,ie 1 followed by 0 followed by 0 followed by no character.

loop 1 : d=10 c not assigned

condition 1) When it enter the loop first condition to be checked or executed is cin.get() which fetches a character in this case '1' .Therefore c='1'

condition 2) c!='2' and d!='4' which is true

condition 3) print d ie here the character '\n' of ascii 10.

So finally you will get a newline. and then d=c

Loop2 : d='1',c='1'

c1) cin.get() therefore c='0'
c2) c!='2' and d!='4' is true
c3) Print d hence prints '1' , then d=c

Loop3: d='0',c='0'

c1) cin.get() therefore c='\n'
c2) c!='2' and d!='4'
c3) print d,therefore prints '0', then d='\n'

Now as there are no more inputs to read in the buffer until the buffer gets newly filled by some other characters the cin.get() waits and hence the while loop waits.
As we have already seen the first input 10 has already been printed and d holds '\n' and waits to print it.

Assume our next input is 42 taken as '4' '2' '\n'

Loop4 : d='\n' ,c= (un-assigned) <same as the first loop condition>

c1) cin.get() therefore c='4'

csurfer 422 Posting Pro

Idea for the topic Yes...Code a simple software for a shop or something.It involves file handling as you need to use them for information write and fetch operations.

And a complete no for the example... Because we want you to code.

csurfer 422 Posting Pro

No you will not have the program code. Such programs are called QUINES if you want the program,read about them and then write.
We are not a bunch of jobless people waiting to code for free.

csurfer 422 Posting Pro

Hi this is the code for life, universe and everything problem, where the program stops only when the user inputs the number "42"

I tried to understand the problem, but could not make it that how the program stops at exactly 42. Any help would be extremely appreciated.

#include <iostream>
 
int main(void) {
char c, d=10;
while(std::cin.get(c) && (c!='2' || d!='4') && std::cout.put(d))
d=c;
}

First thing who told you to take it as a character...??? You just want to stop on 42 right take it as a integer and compare.

csurfer 422 Posting Pro

Ok so you incremented the pointer C after the second if statement...??? Good now it points to the '\0' if it is in the language.
Now in your code you are just returning the control and not the pointer position and as a result the c pointer in main is still at the first position which is the prime cause of your problem.
After calling the test function if you could some how return the pointer position and store it in pointer of main your work is done.

csurfer 422 Posting Pro

Sorry about the reply... I didn't see it was a 6 month old thread resurrected... just answered it... ;)

csurfer 422 Posting Pro

Your code snippet can be shortened tux...here it goes:

void reverse(char p[])
{
     int len=strlen(p);
     for(int i=len-1, j=0;j<i; i--, j++)
     {
          // exchange elements
          p[i]^=p[j]^=p[i]^=p[j];
     }
}

Try it and do tell me what do you think...???

csurfer 422 Posting Pro

csurfer...howz that possible...i mean can u give the syntax plz...


william...this isnt working..i tried it out...

Well what I meant was something like this :

#include<stdio.h>
int main()
{
       printf("%d%d",printf("Hello Daniweb"),printf("I am new here"));
       return 0;
}

You would get an output as I am new hereHello Daniweb1313 The inner printf's are not using a semicolon as you can see.

"printf" function returns the count of characters it has successfully output on the console and it processes from right to left internally hence first it print I am new here and then Hello Daniweb and then count of the successfully output characters of each string which in this case is 13 13.

csurfer 422 Posting Pro

@dangutdavid and Phil++ :

Both of you need to understand your basics well because without it you cant code anything.

Error 1 : Line 7 : Initializing A even before declaring it.
Error 2 : Line 38 : getch() ???

And please tell me that this program has no logic,the do while loop is a complete mess.

A=I;
A=A+I;

=> A=2*I or I+I and nothing else.

M=P-I;
P=P-M;

=>M=P-I
=>P=P-M=>P-(P-I)=>I

==>M=P-I
==>P=I

j=j+1
j was initially uninitialised hence it would be garbage + 1.

csurfer 422 Posting Pro

This OP deserves an applaud, he is so honest that he is directly giving us his assignment copy and asking us directly to code for him. :D

Really sorry,here your honesty doesn't pay you the code you need.You need to work.Work hard and code yourself.

csurfer 422 Posting Pro

There are several problems and it starts from main() only.

1)

char *c;
*c = line [0];

Assigning a character value to an address which doesn't point to any memory.
According to your code this line has to be:

char *c;
c = &line [0];
//or char *c = &line[0];

2) ++*c just fetches the value at which the pointer is pointing to (in this case 'a') and increments the value.But what you want is to move the pointer one step forward to point to the next input so just c++ or ++c is sufficient.

3)Your second if statement within the test function does check the second input character and verifies whether it is '=' or '-' and directly returns true.Problem here is that pointer c is still pointing to the second character only and not the third character.
Another major point is when the function test returns,it returns true but not false in any case and as the pointer c in function test and function main are different,after control returns from test function,pointer c is still at the first character.