Im trying to make a header file that sorts items in an array, I followed the way microsoft.learned did but it doesn't work [closed] - c++

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 3 days ago.
Improve this question
My problem with this is in the program file, it needs a size for my y[] array, but when I put one in, it outputs errors, im really new to c++ and I can't really find the solution, is the problem in my header file?.
//my header file SORTING.h
namespace SORTING
{
class Sort
{
public:
void SORT(int NUMBEROFDATA, float ARRAYCONTENTS[], float TEMPORARY);
};
}
//my header.cpp SORTING.cpp
#include<iostream>
#include"SORTING.h"
using namespace SORTING;
void Sort::SORT(int NUMBEROFDATA, float ARRAYCONTENTS[], float TEMPORARY)
{
for(int INITIALIZERFORTHELOOP = 0; INITIALIZERFORTHELOOP < NUMBEROFDATA; INITIALIZERFORTHELOOP++)
{
for(int SECONDINTIALIZER = INITIALIZERFORTHELOOP + 1; SECONDINTIALIZER < NUMBEROFDATA; SECONDINTIALIZER++)
{
if(ARRAYCONTENTS[SECONDINTIALIZER] < ARRAYCONTENTS[INITIALIZERFORTHELOOP])
{
TEMPORARY = ARRAYCONTENTS[SECONDINTIALIZER];
ARRAYCONTENTS[SECONDINTIALIZER] = ARRAYCONTENTS[INITIALIZERFORTHELOOP];
ARRAYCONTENTS[INITIALIZERFORTHELOOP] = TEMPORARY;
}
}
}
}
//my program.cpp Sort_Program.cpp
#include<iostream>
#include"SORTING.h"
using namespace SORTING;
int main(){
int x = 5;
float y[x];
float z;
y[0] = 10;
y[1] = 7;
y[2] = 5;
y[3] = 11;
y[4] = 9;
Sort Array;
Array.SORT(x, y[], z);
return 0;
}

Replace y[] with simply y when calling Array.SORT():
Array.SORT(x, y, z);

Related

Here it is the program of the printing all the permutation of the string all storing it in the vector but it is printing but not stored why? [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 5 months ago.
Improve this question
Here i want to store the all the permutation of the string in the vector but its only printing
#include <bits/stdc++.h>
using namespace std;
vector<string> str;
void swap(char* x, char* y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
void permute(char* a, int l, int r)
{
int i;
if (l == r) {
cout << a << endl;
str.push_back(a);
}
else {
for (i = l; i <= r; i++) {
swap((a + l), (a + i));
permute(a, l + 1, r);
swap((a + l), (a + i)); //backtrack
}
}
}
/* Driver program to test above functions */
int main()
{
char str[] = "ABC";
int n = strlen(str);
int l = n;
permute(str, 0, n - 1);
for (int i = 0; i < 6; i++) {
cout << str[i] << endl;
}
return 0;
}
Here in the line no 16 i am storing the string in the vector and in the line 35 and 36
i am trying to print all the permutation again using the loop but its not working
while rest of the code is the working the fine .So help me if i made any mistake.
You defined a vector<string> named str in global. Then you defined a char str[] named str as well in main function. So in the scope of main, invoking str corresponds to the char str[] but vector<string>. Just try to use different variable names.
Because you are accessing char str[], instead of vector str.
Avoid using same name for global and local variables.
A Global variable is, as it might sound, a variable that is Global to the code.
That means, you can access it from every scope within the code.
Noticing your code, you have been repeating the variable 'str' both in main and as global.
Conclusion: you should change one of the variable's name.

What is the problem of c++ no value sort()? [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 2 years ago.
Improve this question
While I was studying algorithms, I was trying to run a sort order, but there was an error.
It says there are no members in the value, but there is an error even if you declare the structure. Do you know why?
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct table{
int startValue;
int finishValue;
};
bool cmp(table f, table s)
{
if (f.finishValue == s.finishValue)
return f.startValue < s.startValue;
else
return f.finishValue < s.finishValue;
}
int main(int argc, char *argv[])
{
int maxTimeTable;
cin >> maxTimeTable;
vector<table>t(maxTimeTable);
for (int i = 0; i < maxTimeTable; i++)
{
cin >> t[i].startValue >> t[i].finishValue;
}
sort(t.startValue(), t.finishValue(), cmp); //Error Occurrence Point
int cnt = 0;
int n = 0;
for (int i = 0; i < t.size(); i++)
{
if (n <= t[i].startValue)
{
n = t[i].finishValue;
cnt++;
}
}
}
You are treating t as a table-object
sort(t.startValue(), t.finishValue(), cmp); //Error Occurrence Point
but it's not, right? t is defined as vector<table>t(maxTimeTable);. And correctly, the compiler provides an error, since vector<table> does not have a method startValue nor a finishValue.
You probably meant to sort the vector by accessing the iterator to the first element, and to the last element:
sort(t.begin(), t.end(), cmp);

Using arrays to find a maximum [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 8 years ago.
Improve this question
I take in an array I go through all it's elements, if any member of the array is greater than j, than it makes that element the new j. For some reason I keep getting back that the maximum is 4. What's going on?
##include <iostream>
using namespace std;
int MAXIM(int arg[],int sz){
int j = 0;
for(int i = 0; i < sz; i++){
if(arg[i] > j){
j = i;
}
}
return j;
}
int main(){
int coolarr[5] = {5,17,45,7,34};
int maxxy = MAXIM(coolarr, 5);
cout << maxxy << endl;
}
j = i; this line is wrong; it should be j = arg[i];.

Array of Structs (expected expression error) 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 8 years ago.
Improve this question
I have the following code. I am trying to populate an array with a deck of cards, and I keep encountering the same error "expected expression error" no matter how I code the loops to populate the array.
Can anybody see where I'm going wrong. I think its something painfully simple, that I, who am new to C++, am just missing.
Thanks!!
#include <iostream>
using namespace std;
struct playingCard{
char suit; // heart (1), club (2), spade (3), diamond (4)
int value; // 1 to 13 (ace is LOW)
};
void printArray(playingCard playingCardArray[], int size){
for (int i = 0; i < size; i ++){
cout << playingCardArray[i].suit << ":\t" << playingCardArray[i].value << endl;
}
}
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 1;
while (suitLoop == 1){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i] = {suitLoop, valueLoop},
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
To resolve your compilation issue change you inner for loop like this:
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[i].suit = suitLoop;
playingCardArray[i].value = valueLoop;
}
Other than compilation your code also has Infinite loop , to resolve this you need to change your main somewhat like this:
int main()
{
const int ARRAY_SIZE = 52;
playingCard playingCardArray[ARRAY_SIZE];
int i = 1;
int suitLoop = 0;
while (suitLoop < ARRAY_SIZE){
for (int valueLoop = 1; valueLoop <= 13; valueLoop++){
playingCardArray[suitLoop].suit = (suitLoop/13 + 1);
playingCardArray[suitLoop++].value = valueLoop;
}
}
printArray(playingCardArray, ARRAY_SIZE);
return 0;
}
Exchanging the comma with a semicolon at the end of playingCardArray[i] = {suitLoop, valueLoop}, solves the problem.

Strange average value of array elements [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 8 years ago.
Improve this question
I am learning C++ arrays. In code below:
double mean(int n, double *a)
{
double sum = 0;
for (int i=0; i<n; i++)
{
sum += a[i];
return sum/n;
}
}
main()
{
double t[]={5, 5};
cout << mean(2,t) << endl;
}
mean returns 2.5, although it should return as 5 as I think.
Does anyone know why 2.5 was returned from mean?
return sum/n; needs to be outside the for.
Step through your code: after adding a[0] to sum, the function returns sum/n, which is 5/2.
Your current code will return the first element of the array divided by the number of elements. To fix it you can just move the return sum / n statement outside of the for loop:
double mean(int n, double *a){
double sum = 0;
for (int i=0; i<n; i++){
sum += a[i];
}
return sum/n;
}
But with the help of the standard library algorithm std::accumulate you can simplify your code to:
inline double mean(int n, double *a) {
return std::accumulate(a, a + n, 0) / n;
}
And here's the working example.
Finally remember to avoid using namespace std and to always specify the return type of the main function. Also, turn on compiler warnings, as you would have been warned that something weird some happening in your code.