Same average function c++ [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to compare averages of an array and to detect if there is any similar averages,
for example if I have these averages 25,30,70,30,60 so the function should return true because 30 is twice there ,but it seems what I'm doing is wrong
bool sameAverage(Student Array[],int size)
{
bool isSame=false;
for (int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
if (Array[i].getAverageGrade==Array[j].getAverageGrade)
isSame=true;
}
}
return isSame;
}
it is showing me a red line under the dot operator

Array[i].getAverageGrade is a function. You can compare that function to another function (like Array[j].getAverageGrade) but what you really want is to
call that function
compare the result to the result of calling the other function:
Array[i].getAverageGrade() == Array[j].getAverageGrade()
BTW: Please keep in mind what others have told you about comparing double values.

What I understand is that, you are looking for duplicates in the array.
First sort your array.
Then, Use a single loop to iterate through the array. Two loops are not needed.
bool sameAverage(Student Array[],int size)
{
for(int i = 0; i < size - 1; i++) {
//Use your favorite way to compare floating point numbers for equality
if ((Array[i].getAverageGrade() - Array[i + 1].getAverageGrade()) < 0.0001) {
return true;
}
}
return false;
}

Related

Declare int variable aux a.length = (); Or use o.length () in all loops? [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 wonder which is faster: Say I'm working with some text (30 characters), which would be better? And with a lot of text which would be better?
1-
int tam = text.length();
for(int i=0;i<tam;i++)
{
//something here//
}
2-
for(int i=0;i<a.length();i++)
{
//something here//
}
and also comparing these two:
1-
for (int i = 0; i < b.length(); i++)
{
aux = a.find(b[i]);
if (aux == -1)
{
sucess = 0;
break;
}
else
{
a.erase(aux,1);
}
}
2-
for (int i = 0; i < b.length(); i++)
{
if (a.find(b[i]) == -1)
{
sucess = 0;
break;
}
else
{
a.erase(a.find(b[i]),1);
}
}
Both first are the better approach.
On the first example you are checking if i<a.length() is true on every cycle. That means that you are going to execute a.length() for every iteration. If the variable a is not changed, it is unnecessary and the better approach is to calculate before and use that value.
Note that if the variable a is changed inside, placing i<a.length() might be the correct approach. It depends on your problem.
On the second example it is the same basics. You avoid useless calculations because you won't need to calculate a.find(b[i]) again inside the else.
As a general rule of thumb, as computations get bigger, more complex, and more frequent you want to minimize your unnecessary calculations. This means that storing something that needs to be calculated in a variable may speed up the process.
In both of your examples, for extremely large numbers,
int scratch = big.length();
for(int i=0;i<scratch;i++){
//body//
}
is usually faster.
In the future, general questions like this tend to belong in something like the Code Review Stack Exchange.

Unexpected changing of C++ constant integer [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 8 years ago.
Improve this question
The variable steady_counter is intialized as a constant integer.
cout << steady_counter;
So long as i have the above statement anywhere before the following code, the function runs as expected and checks if an integer input is or is not a runaround number.
The problem is that when the cout line is not present, the constant integer changes within the below if statements. I tested this by printing steady_counter before entering the if-else, and then after the if-else.
Without the cout line, steady_counter changes to a 4 digit number.
for (int i = 0; i < 10; i++)
{
if (CheckArr[i])
{
num_of_unique++;
}
}
if ((steady_counter == num_of_unique) & (final == NumArr[0]) )
{
return true;
}
else
{
return false;
}
}
Any idea what's going on? Why do I require a cout line to maintain the constant integer steady_counter?
One obvious problem:
for (int i = counter; i > 0; i --)
NumArr[i] = -1;
This covers values from 1 to counter inclusive; while valid indexes for NumArr are from 0 to counter-1 inclusive. So you write outside the array, corrupting something else; possibly another local variable.
Either correct the off-by-one error in the index
NumArr[i-1] = -1;
or use a more canonical loop
for (int i = 0; i < counter; ++i)
or, for more of a C++ flavour,
std::fill(NumArr, NumArr+counter, -1);
There are likely to be further errors, which are better found by using your debugger than by asking people to read through all your code.

Is if statement over complicated? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
This is the part that brakes, zod1 zod2 zodA are vectors. In vectors I store int. Cant tell why it crashes cause it gives no error
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
Questions:
1 Is if statement over complicated
2 Can I use vectors like that for solving math in if statement (sorry can't explain better)
3 If 2 question is true how should I solve it
Explanations:
variable names are in my own language (sorry if it looks weird)
Values are zero for all (ex zod1[u]=0)
added whole function (variables going to the function are passed correctly and I know I use some unnecessary thing)
void calc(vector<char> zodis1, vector<char> zodis2, vector<char> zodisAts,int zo1,int zo2,int zoA)
{int i,keliamas=0;
int k =0;
vector<int> zod1(0);
vector<int> zod2(0);
vector<int> zodA(0);
for(i=0;i<zodis1.size();i++)
{
zod1.push_back(0);
}
for(i=0;i<zodis2.size();i++)
{
zod2.push_back(0);
}
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0);
}
int u=zodis1.size()-1;
int y=zodis2.size()-1;
int t=zodisAts.size()-1;
if(zod1[u]+zod2[y]==zodA[t] || (zod1[u]+zod2[y])%10==zodA[t])
{//if((zod1[u]+zod2[y])/10==1)
{
keliamas=1;
}
if(u==0||y==0||t!=0)
{
if(keliamas==1)
{
}
}
u--;
y--;
t--;
}
else
{if(zod1[u]!=9)
zod1[u]=zod1[u]+1;
else
{ if(u!=zodis1.size()-1)
u++;
else
{
cout<<"something wrong man";
}
}
}
}
EDIT:
Error is here:
for(i=0;i<zodisAts.size();i++)
{
zod2.push_back(0); // should be zodA
}
Is if statement over complicated
No, I think it is very simple with just one 'or'. However, if zodA[t] is always less than 10, then your if condition can be written as:
if ( (zod1[u]+zod2[y])%10==zodA[t] )
Can I use vectors like that for solving math in if statement (sorry
can't explain better)
Yes you can.
If 2 question is true how should I solve it
If it compiles but then crashes, then probably you are accessing out of bounds indices. Check that your indices are less than the vector sizes.

how to get correct answer merge 2 sorted arrays?! C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.

How can I rewrite this function in non-recursive form? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
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.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
How can I rewrite this function in non-recursive form?
void generate(int pos)
{
if (pos == n + 1)
{
print_table();
}
else
{
for (int i = 1; i <= n; i++)
{
if (!used[i])
{
used[i] = true;
perm[pos] = i;
generate(pos + 1);////recursion
used[i] = false;
}
}
}
}
This code appears to call print_table() for each permutation of the numbers 1,...,n. There is a built-in tool for this in C++.
#include <algorithm>
void generate() {
int n = 10; // or whatever
std::vector<int> perm(n);
for(int i=0; i<n; i++) perm[i] = i+1;
do {
print_table(perm);
} while(std::next_permutation(perm, perm+n));
}
Your code seems to be a standard recursive algorithm for generating all permutations of a list of elements. Rather than trying to mechanically massage the recursive algorithm into an iterative one (which would probably require a stack of some sort), you might want to look at iterative algorithms for listing off all permutations of a list. For example, C++ provides the std::next_permutation algorithm, which you can use to list permutations. For reference, I have a simple implementation of this algorithm along with commentary explaining how it works.
Hope this helps!