C++ : How to arrange 4 values in an ascending order? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hello I know it may be a beginners question but I need help.
I need to compare between 4 values added by the user and arrange them in am ascending order by using a function that takes 2 inputs and return the smaller one. I know it can be done by arrays but I must not do it. I already have the function but I don't know how to use to do the trick without having a very long code. Thanks

This seems to me to be an obvious "homework question," so let me answer it cryptically in order to maybe push you in the right direction.
First, the hint: divide and conquer.
Second hint: the "Towers of Hanoi" problem.
You have a function that can compare two values. Okay, then: "four elements" can be viewed as "two groups of two values each." Given that either of the two input to your comparison-function can be the result obtained by a nested call to the same function . . . you can, indeed, solve this problem, in one line of code, without using arrays.
I'm trying here to "teach you to fish," so I'm not handing you the fish on a platter.

If you know c++ then you can use sort function. But for this you have to include algorithm as:
#include <algorithm>
and sort function will be used as:
sort(array, array+N);
where array is the array name and N is the size of array.After this operation you will get a sorted array in ascending order and return first element.Now the function will look like as:
int smallest(int *array) {
int size = sizeof(array) / sizeof(array[0]);
sort(array, array+size);
return (array[0]);
}
And now call this function from main()

Related

How does this code works in c++? [closed]

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 6 years ago.
Improve this question
I participated in Codeforces Round #396 (Div. 2) yesterday.
The (A) problem seemed pretty advanced in my opinion when I read it.
I tried solving it all the competition time yesterday and some time today.
I came up with a 200 lines long(half-working) solution. And then I gave up.
I looked what other people wrote there and I saw max 20 lines long code that seems magic to me.
The problem asks you to output the length of the longest uncommon subsequence of letters from two strings.
You can read the full problem here:
http://codeforces.com/contest/766/problem/A
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main(){
cin>>a>>b;
printf("%d",a==b?-1:max(a.size(),b.size()));
return 0;
}
This is all the code used to solve the problem, and I really want to know how that one line of code
printf("%d",a==b?-1:max(a.size(),b.size()));
can solve this "advanced" task?
If the two strings are equal, there is no "uncommon subsequence". If they are not equal, neither one is a subsequence of the other, but each one is a subsequence of itself, so each one is an "uncommon subsequence". The longer of the two is the longest "uncommon subsequence", and its length is the correct answer. If the two are not equal but have the same length, then each one is an "uncommon subsequence" and the length of the longest is just the length of either one.
Don't get tangled up in what you (or any other reasonable person) think a "subsequence" is. The problem defines "uncommon subsequence", and all you have to do is apply its definition. This "problem" is about word play, not coding.
It does not seem to solve the task at hand. It just outputs -1 when the strings are equal and the length of the longer string when they are not different. However, the problem asks for the longest uncommon sequence. It would fail with the inputs abc and abcd to give the answer 1 would rather give 4.
What the line does is the following:
It checks whether a == b, whether the strings are equal.
If they are equal, the condition A is true in the ternary operator A ? B : C, so that expression will evaluate to B which is just -1 here.
If they are not equal, it will evaluate to C which is the maximum of the two string lengths, so it will be length of the longer string.
The value of the ternary expression is then printed, albeit without a newline, which is a bit bad.
So the code is so short because it only solves the problem for a few cases but not the general case.
printf ("%d", a == b ? -1 : max (a.size (), b.size ()));
is equivalent to
if (a == b) {
printf ("%d", -1);
}
else {
if (a.size () > b.size ()) {
printf ("%d", a.size ());
}
else {
printf ("%d", b.size());
}
}
BTW That's only a small step in solving the problem...
[CORRECTION]
IT DOES SOLVE THE WHOLE PROBLEM (See answer of Pete Becker)

what does this error mean in general? and how I fix it in this case? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
The error message appears on Xcode that says "invalid operands to binary expression.
in my code I'm using an array of a struct, i'm trying to sort input data in an ascending order, and i'm getting this error message at the "if" condition shown in the print screen at this link:
https://www.dropbox.com/s/0mch2gbxcif0a20/Screen%20Shot%202016-04-27%20at%2012.45.45%20PM.png?dl=0
The Code
if (studentsInfo[i] > studentsInfo[i + 1]) {}
The Error
Invalid operands to binary expression ('students' and 'students')
What do you compare in your program? As I see, you have to compare names, but all you do is compare an array element which is a struct data type.
If you are trying to compare names, you have to use dot "." operator to reach names. After yo compare names, you can change the elements's place.
The error means that > only takes two arguments and you are using it for something else. In this case you are comparing an entire data structure that does not have an override for > operator and is an undefined behavior. StudentsInfo[i] is a data structure that has more than one element in it. Replace the StudentsInfo[i] with StudentsInfo[i].GPA or another element whose data type has a defined > operator.

C++ Function Prototype? [closed]

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
I had this on a test this week and got it wrong. I asked the professor for help and he said we'd go over it next Thursday. I really don't want to wait that long. Can any one on here walk me through it?
Perform the task specified by the following statements:
Write the function prototype for function "doit" that takes an integer array parameter "list" and an integer "size" parameter and returns a boolean value.
Thanks in advance for any help
bool doit(int list[], int size);
The function doit takes an array of integers as first parameter, and the size of the array (which is an integer) as second parameter. It returns a boolean (true or false).
This sort of function prototype is typically used to access each element of the array within a for loop (with size as terminating condition). The boolean return value could inform of the presence or absence of some value in the array for example, or if some work could or could not be performed.
A little bit of help is there :
RETURN_TYPE FUNCTION_NAME (ARGUMENT1_TYPE ARGUMENT1,ARGUMENT2_TYPE ARGUMENT2);
It's not a big deal , it's just a prototype :)
I wrote the answer and removed it, u should try looking for the answer in the internet :)
Here you go:
+ doit(list : int[], size : int) : boolean

C++ Multi-Dimensional Array saving/loading to file error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I've been working on this C++ project for roughly 2 weeks now and I'm stumped on a couple things regarding 2D Arrays. To start off here is the code I wrote as of now:
http://pastebin.com/vCsz947Q
I decided to provide this as a pastebin link due to the fact that it's rather large (it uses 8 functions) and I thought it would save space in this post. I'm new to this site so I apologize if there's a better way that I'm not aware of.
The Problem:
When I go to save my "char seating" array to a .dat file using the "save seats" function that I created, I get a file that gives me the following garbage character " Ì " instead of the intended ' # ' (for open seats) or ' * ' (if a seat is bought).
My functions will save the intended amount of rows (15) and columns (30) despite this though. Also an asterisk will be placed when I go to "purchase a seat" in this program in the file. Additionally my program loads the files as intended, except for the fact that... Well... There's garbage data stored in the seat array.
I feel like this relates to another problem I'm having where if I go to the "purchase seats" function and I say to purchase a seat, it should replace a # with a *, but it doesn't, yet in the saved file it will show an asterisk in the intended spot... Which is very strange.
I have absolutely no idea why this occurs, and what's frustrating is this one thing that's preventing me from finishing this program. I want to believe that my original array in int main that's being called by other functions isn't being updated properly, but I don't know, which is why I came here to seek assistance.
Thank you for your assistance whoever can help.
Well for a start you have some undefined behaviour here inside your displaySeatingChart (char displaySeats[ ][30], float displayPrices[ ]) function with the following:
const int rowDisplay = 15;
const int colDisplay = 30;
as later within one of your loops you have
cout << displaySeats[rowDisplay][colDisplay];
which is clearly reading beyond the array bounds since in main() you define
const int rowMain = 15;
const int colMain = 30;
char seating[rowMain][colMain];
float seatPrices[15];
and pass both seating and seatPrices to the displaySeats function. There may well be other problems with your code but this at least is a clear example of undefined behaviour. Consider stepping through the code with a debugger to get a clearer idea of the source of the issue.
On another note given that you are working with C++ consider working with std::vector instead of arrays. This will give you more scope to ascertain the dimensions of the items that arrays that you are working with from within your utility functions and result in less potential for errors in array access.

Haskell intersect two lists and remove double elements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm new to Haskell and need an function to intersect two lists, but remove double elements.
If I load the Data.List
import Data.List
I can use this function:
intersect "abbcd" "abbe"
"abb"
The outcome is "abb", but what I need is:
intersectFunction "abbcd" "abbe"
"ab"
So the double char from the lists should display only once.
Any ideas?
perhaps the "unique" function named nub can help:
import Data.List
intersectFunction a b = nub $ intersect a b
You can use the intersect function you have, but then you need to remove the duplicate elements. It seems likely that there's a library function to do this, but how can we find it if we don't know the name?
The best way to answer questions of the form "Is there a Haskell function to do X" is usually:
Figure out what the type signature of the function you want would be.
In this case, we want a function that takes a list and produces a list. So the type signature is [a] -> [a]
Search hoogle or hayoo. If you don't find it in one, try the other. Usually it doesn't matter much whether you get the order of the input parameters exactly right, but you may need to experiment.
In this case, there are quite a few functions with that type signature. But if you go to the second page of results, you'll find the nub function, which does what you need.