Click here to Skip to main content
15,917,456 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Create a C program that tests whether two sequences of positive numbers are order equivalent.

Two sequences a and b are order equivalent if they have the same number of elements and
they both contain less than two numbers or if for all where n is the length of a sequence

Examples of order equivalent sequences:

7 5 9 13 12 and 4 2 25 33 26
12 and 8


The program must accept the two sequences with no separator between the two. If the number of numbers accepted is n, the first n/2 numbers are assumed to be in the first sequence and the rest form the second sequence. The program must then check the sequences for order equivalence and print the result. You can assume that the maximum size of the two sequences is 100. You need to use 0 or any negative number to mark the end of the sequence.

I have C# code but I want that in c

What I have tried:

Console.Write("input: ");
var input = Console.ReadLine();
var nums = input.Split(' ').Select(s => int.Parse(s)).ToList();
if (nums.Count % 2 != 0) {
    throw new InvalidOperationException("Length must be even.");
}
var seq1 = nums.GetRange(0, nums.Count / 2);
var seq2 = nums.GetRange(nums.Count / 2, nums.Count / 2);
Console.WriteLine("seq1: " + string.Join(" ", seq1));
Console.WriteLine("seq2: " + string.Join(" ", seq2));
bool areEquiv = true;
for (int i = 0; i < seq1.Count && areEquiv; i++) {
    for (int j = 0; j < seq2.Count && areEquiv; j++) {
        if ((seq1[i] <= seq1[j]) != (seq2[i] <= seq2[j])) {
            areEquiv = false;
        }
    }
}
Console.WriteLine(string.Format("Are {0}order equivalent", areEquiv ? string.Empty : "NOT "));
Posted
Updated 16-Sep-16 3:01am
v2
Comments
Mehdi Gholam 16-Sep-16 8:44am    
Looks like homework.
[no name] 16-Sep-16 8:49am    
This is your homework not ours and we are not a code translation service.
Danyal Awan 16-Sep-16 8:58am    
Thank You

A simple approach woud use an array of 100 ints for each sequence.
Once you have read the input into the arrays a loop is enough to establish the equivalence.
 
Share this answer
 
v3
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
And don't find random code on the internet in a different language and assume that is will be perfect in (for example) C - it won't. What works well as a solution in one language is poor code in another. For example, you don't normally use pointers in C# - in C you use them a LOT. In C#, you use events, delegates, exceptions, and Linq extensively - none of those exist in C.
 
Share this answer
 
Comments
Danyal Awan 16-Sep-16 8:58am    
Thank You
OriginalGriff 16-Sep-16 9:10am    
You're welcome!
We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
So, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.
 
Share this answer
 
Comments
Danyal Awan 16-Sep-16 8:58am    
Thank You

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900