Jump to content

The Programming thread


Dienekes

Recommended Posts

The Android SDK tutorials are fine but I suppose you have to ask yourself - what do i want to finally accomplish and then go looking for stuff related to what you need to do.

Link to comment
Share on other sites

  • 1 year later...

My current project involves a Monitoring application reading data (via USB) from an embedded device, and then interpreting that data and displaying graphs among other things.

 

I created a Custom Emulator to emulate the functions of the embedded device, such that the Emulator reads data from a CSV file and sends data out through a USB comport.

 

I need the Emulator and the Monitoring application on two separate computers, and preferrably talking via USB. Is there any way I can do this?

 

Scenario: Emulator reads data from CSV and sends data out of a port to which the Monitoring PC is connected via USB. The Monitoring PC receives this data and the Monitoring application will interpret data as if it received it from the embedded device.

 

I tried connecting the two PCs with a USB male-male cable, but the PCs don't detect eachother. Is there anything I'm missing? Like drivers? or switching the send/receive pins/signals?

Link to comment
Share on other sites

^^ USB comport? Did you mean emulating serial over USB? If yes, you'd need a serial converter chip (FTDI and vishay are two famous makers) for this in the cable you are using and sending/receiving data would be very simple but limited to the max baud rate of the serial port.

Direct USB communication is a big beast. It won't work directly by just connecting two PCs together. You'd need one PC to act as a host and the other as a device. You also need to decide which kind of device will you emulate etc. You can search for a program that allows to act as a filer device or something or may need to write your own drivers (USB drivers are also PITA btw, there are several layers that you need to take care of).

Network connection would be the easiest and would not require any special peripherals. You can just use basic socket programming (read first few chapters of stevens) to connect and send/receive data.

For displaying graphs, you could either search for tons of graphing libraries available for all languages or use graphviz. I use graphviz for all my graphing/plotting needs. It has a very simple interface and you can also generate .dot files (text files in special graphviz format) which it can read at runtime to create the graphs

Link to comment
Share on other sites

^^ USB comport? Did you mean emulating serial over USB? If yes, you'd need a serial converter chip (FTDI and vishay are two famous makers) for this in the cable you are using and sending/receiving data would be very simple but limited to the max baud rate of the serial port.

Direct USB communication is a big beast. It won't work directly by just connecting two PCs together. You'd need one PC to act as a host and the other as a device. You also need to decide which kind of device will you emulate etc. You can search for a program that allows to act as a filer device or something or may need to write your own drivers (USB drivers are also PITA btw, there are several layers that you need to take care of).

Network connection would be the easiest and would not require any special peripherals. You can just use basic socket programming (read first few chapters of stevens) to connect and send/receive data.

For displaying graphs, you could either search for tons of graphing libraries available for all languages or use graphviz. I use graphviz for all my graphing/plotting needs. It has a very simple interface and you can also generate .dot files (text files in special graphviz format) which it can read at runtime to create the graphs

Hmm... :scratchchin: I already have the two applications that I need. Forget about what I told you about the emulation part before. I'll try again :) I have an application A that sends data out through USB, and an application B that receives and interprets data that it receives from a USB. In both applications, I just display the available

comports, and let the user select the port from which to send/receive data.

 

The thing is, I need to have these applications on two different computers and talking to each other, as in, the application B has to receive data sent out from application A. I thought the best way to do it might be just to use a USB male-male cable, but it looks like it's not as easy as that. How do I configure one PC to be the host and the other to be a client? :scratchchin:

 

If this doesn't work, yeah, I could try doing a network connection. I'll lookup how to do some basic socket programming meanwhile.

Link to comment
Share on other sites

^^ +1 to what shantz said .... socket communication is like 10x simpler if you just want 2 programs (in 2 diff computers) to talk to each other .... you can refer to this online tutorial if are new to socket / network programming and want to get started right away !

Link to comment
Share on other sites

  • 1 year later...

I went with what Shantz said and got a Null modem cable from FTDI, and that worked perfectly well for the communication. Thanks guys.

 

I have a pretty basic (probably silly) doubt about Linked Lists. When you want to check if a singly linked list is cyclic, all the suggestions are about using two pointers, with one moving two nodes at a time and the other one moving one node at a time. If there's a cycle, the one that goes two steps will meet the slower one eventually.

 

My doubt is, why not just have two nodes with one just sitting at the starting point, and the other one moving one node at a time forward till it goes around to meet the stationary one? Is there anything wrong with doing this? :scratchchin:

Link to comment
Share on other sites

^^ I'm not

 

It won't always be the case that the starting node is part of the cycle in the linked list.

circular-linked-list.gif

 

:scratchchin: What do you mean by that? Isn't a circular linked list just a linear linked list with the last node's Next pointing to the first node?

 

Do you mean the case where the last node points to a node in the middle of the list? :scratchchin: That would be a weird 'circular' linked list :scratchchin:

 

Thank you

Link to comment
Share on other sites

^^ circular ll is more of a problem in the context that you mentioned. It was not the intent. In an otherwise linear list, it happens at times.... mainly due to wrong usage of pointers. And when it happens it can happen anywhere. I've seen cases where the node was pointing to itself causing infinite loops.... Your ex works only when the loop is at the first node, what if the loop is in the mid ?

 

Sent from my Nexus 5 using Tapatalk

Link to comment
Share on other sites

  • 4 weeks later...

Cannot seam to understand this thing -

Geeko is in worry now because exam is coming up and he has to know what rank he can get in exams. So he go back into the school records and finds the amazing pattern.[/size]

He finds that if a student is having a current rank n than his rank in the final exam will be the count positive numbers between in the range [1,n] which are relatively prime to n


As being geek he became curious now he want to calculate the rank of all his classmates in final exam, but he finds this task a bit hard , So he ask you programmers to solve this task for him.

Input:
The first line of each test file contains a integer t denoting the number of test case.Each test case contains a numbers n representing the current rank of each student

Output:
for each test case output single integer the rank of student in final exam in new line.

Constraints:

1<= t <= 2000
1<= n <10^6


Sample Input (Plaintext Link)
2
5
4

Sample Output (Plaintext Link)
4
2







My code -

#include <iostream>
using namespace std;

int main()
{
    int t,p=0,a=0;
    cin>>t;
    for(int j=1;j<=t;j++)
    {
		p=0;
       for(int i=1;i<=j;i++)
       {
            if((t%i==0)&&(j%i==0))
			{
				p++;
			}
       }
    if(p==1)
    a++;
    }
cout<<a;
return 0;
}

Firstly their sample output is weird, its 5 =4 and 4 = 2 but what about 2 = ??
Their solution -

#include<stdio.h>
#define ll int
int scan()
{
   int t=0;
   char c;
   c=getchar_unlocked();
   while(c<'0' || c>'9')
       c=getchar_unlocked();
    while(c>='0' && c<='9')
     {
        t=(t<<3)+(t<<1)+c-'0';
        c=getchar_unlocked();
     }
  return t;
}   
ll phi(ll n)
{
     ll result = n;
     ll i;
       for(i=2;i*i <= n;i++) 
       { 
         if (n % i == 0) 
         result -= result / i; 
         //printf("%lld\n",result);
         while (n % i == 0) 
         n /= i; 
       } 
       if (n > 1)
       result -= result / n; 
       return result; 
}
int main()
{
    int t;ll num;
    t=scan();
    while(t--)
    {
        num=scan();
        printf("%d\n",phi(num));
    }
    return 0;
}

Link to comment
Share on other sites

Your code is correct if the input asked for is only the rank of student. But you needs to give 2 types of input.

 

Inputs should be like this

 

1. t= no. of input ranks you wanna give

2. n1= rank of student 1

3. n2= rank of student 2

.

.

t+1. nt= rank of student t

 

Output will be

 

1. new rank of student 1

.

.

.

t. new rank of student t

 

 

Input

2- wanna give 2 students rank

5- 1st student's rank

4- 2nd "

 

Output

4

2

 

hope that helps :fear:

Link to comment
Share on other sites

Your code is correct if the input asked for is only the rank of student. But you needs to give 2 types of input.

 

Inputs should be like this

 

1. t= no. of input ranks you wanna give

2. n1= rank of student 1

3. n2= rank of student 2

.

.

t+1. nt= rank of student t

 

Output will be

 

1. new rank of student 1

.

.

.

t. new rank of student t

 

 

Input

2- wanna give 2 students rank

5- 1st student's rank

4- 2nd "

 

Output

4

2

 

hope that helps :fear:

 

Wrote a code -

 

#include <iostream>

using namespace std;
void test(int t)
{
int p=0,a=0;
for(int j=1;j<=t;j++)
{
p=0;
for(int i=1;i<=j;i++)
{
if((t%i==0)&&(j%i==0))
{
p++;
}
}
if(p==1)
a++;
}
cout<<a<<endl;
}
int main()
{
int n,x,t;
cin>>n;
x=n;
while(x!=0)
{
cin>>t;
test(t);
x--;
}
return 0;
}

 

and on my test run, it took 0.1005 seconds to run (on their site's compiler) and 64kb memory but when i submitted it to them, those faggots are like 8+ seconds runtime and 240+ kb memory....

13z1y7a.jpg

faggots...

Link to comment
Share on other sites

Yeah thats looks like correct code. You also need to take into account that n and t values constraints. With int you won't be able to run code for n=10^5 and above.

 

Maybe their compiler must be diff from the one u used.

 

Not able to understand there code :fear:

Link to comment
Share on other sites

This is my solution for the problem. The compiler finds it incorrect & times out.

 

Working correctly for the sample input & few custom inputs I've tried. Strange.

import java.util.Scanner;
import java.util.ArrayList;

class ExamTime {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		
		for(int i=0; i<n; i++) {
			int count = 0;
			int cr = Integer.parseInt(sc.nextLine());
			for(int j=1; j<cr; j++) {
				if(EuclidGCD(cr, j) == 1) {
					count++;
				}
			}
			System.out.println(count);
		}
		
		//System.out.println(EuclidGCD(5, 3));
	}
	
	public static int EuclidGCD(int a, int  {
		int temp;
		while(b != 0) {
			temp = b;
			b = a % b;
			a = temp;
		}
		return a;
	}
}

Used Euclid's GCD Algorithm to find if numbers are relatively prime or not. (Prime if GCD(a,B) == 1)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...