Hello all. So I\'m new to C++ and I\'ve spent 3 days trying to make the code bel
ID: 3605096 • Letter: H
Question
Hello all. So I'm new to C++ and I've spent 3 days trying to make the code below compile!! If anyone can help me fix it, I'll be extrmrely thankful as the final code is due tonight. Thank you!
#include <iostream>
#include <time.h>
#include <fstream>
#include "FileFunctions.h"
#include "Statistics.h"
using namespace std;
//write data to .txt file
void WriteRandomData(int N, int M, const char *filename)
{
int i;
int j;
int l;
char* filename = new char;
filename = (char*) filename;
ofstream outputfile;
while(i=0, i<=N, i++, j>0, j<=M)
{ srand(time(NULL)); //sync with time
j = rand() % M; //pick a random number from 0 to M
while(i<N)
{
outputfile.open(filename);
for(int l=0;i<=M;l++)
{
outputfile << j << " ";
}
}
}
outputfile.close();
return filename;
}
//read data and find size
void ReadData(const char *filename, int &size, int myArray)
{ int p;
int myArray = new int[p];
WriteRandomData(int N, int M, const char *filename);
char k;
int
outputfile.is_open();
size = 0;
int l;
while(k != EOF, p=0, p++ )
{
getline(outputfile, k);
l << k;
myArray[p] << l;
if(k == EOF)
{
break;
}
}
size = sizeof(myArray)/sizeof(myArray[0]);
cout<< size;
outputfile.close();
return;
}
}
//find median value
double getMedian(int myArray, int &size) {
// Allocate an array of the same size and sort it.
int myArray = new int[i];
WriteRandomData(int N, int M, const char *filename);
ReadData(const char *filename, int &size, int myArray);
double* arraysorted = new double[size];
for (int i = 0; i < size; ++i) {
arraysorted[i] = myArray[i];
}
for (int i = size - 1; i > 0; --i) {
for (int j = 0; j < i; ++j) {
if (arraysorted[j] > arraysorted[j+1]) {
double arrayTemp = arraysorted[j];
arraysorted[j] = arraysorted[j+1];
arraysorted[j+1] = arrayTemp;
}
}
}
// Middle or average of middle values in the sorted array.
double arrayMedian = 0.0;
if ((size % 2) == 0) {
arrayMedian = (arraysorted[size/2] + arraysorted[(size/2) - 1])/2.0;
} else {
arrayMedian = arraysorted[((size +1)/2)-1];
}
cout << arrayMedian<< endl;
}
//get largest value
int getLargest(int myArray, int &size)
{ int myArray = new int[k]
getMedian(int myArray, int &size);
for (int k = 0; k < size; ++k)
{ if( myArray[k] > myArray[0])
{ cout << myArray[k];
}
}
}
// write the array forward and then backwards: [1 2 3 4 5 5 4 3 2 1]
int *DoubleandReverse (int list, int &size)
{ int r;
int myArray = new int[r]
riteRandomData(int N, int M, const char *filename);
double* arraysorted
std::string q;
int r,t,v;
int u = size-1;
int s = 2 * size;
while(r=0, r<&size, r++)
{
q += myArray[r];
}
while(t=0, t<size, t++, u<s, u++)
{
q[u] += q[t];
}
while(v=0, v<s, v++)
{
list[v] += q[v];
}
int i;
while(i=0, i<size, i++){
cout << '[' << list[i] << ']'<<endl;}
return 0;
}
#include <iostream>
#include "FileFunctions.h"
#include "Statistics.h"
using namespace std;
int main()
{
const char* name;
cout<< "Please enter filename:";
cin >> name;
int double_reverse = DoubleandReverse (list, &size);
int arraysize = ReadData(name, &size, myArray);
double median = getMedian(myArray, &size);
int largest = getLargest(myArray, &size);
cout << "Writing file: "<< name << endl;
cout << "Reading file: "<< name << endl;
cout << "Array size: "<< arraysize << endl;
cout << "Doubled and Reversed: " << double_reverse << endl;
cout << "Median is: " << median << endl;
cout << "Largest is: " << largest << endl;
return 0;
}
Explanation / Answer
Your code has a lot of mistakes, therefore, it was not possible to modify the code. So, I've written the function definition freshly. Please compare your code and mine and notice every single detail to properly understand your logical mistakes. This way, you can develop your C++ knowledge.
FileFunctions.h:
#ifndef FILEFUNCTIONS_H
#define FILEFUNCTIONS_H
void WriteRandomData(int N, int M, const char *filename);
void ReadData(const char *filename, int &size, int *myArray);
#endif // FileFunctions
FileFunctions.cpp:
#include <ctime>
#include <cstring>
#include <fstream>
#include <cstdlib>
#include "FileFunctions.h"
using namespace std;
//write data to .txt file
void WriteRandomData(int N, int M, const char *filename)
{
int i, j;
ofstream outputfile;
outputfile.open(filename); // Opening output file in write mode
srand(time(NULL));
for(i = 1; i <= N; i++){
j = rand() % (M + 1); // Generating random numbers from 0 to M inclusive
outputfile << j <<endl;
}
outputfile.close(); // Closing and saving file
}
//read data and find size
void ReadData(const char *filename, int &size, int *myArray)
{
char temp[20];
ifstream inputfile;
inputfile.open(filename);
size = 0;
while(inputfile.getline(temp, 20)){ // Counting lines
size++;
}
inputfile.close();
inputfile.open(filename);
myArray = new int[size]; // Create array
int i = 0;
// Read numbers to array
while(inputfile.getline(temp, 20)){
myArray[i++] = atoi(temp);
}
inputfile.close();
}
Statistics.h:
#ifndef STATISTICS_H
#define STATISTICS_H
double getMedian(int myArray[], int size);
int getLargest(int myArray[], int size);
#endif
Statistics.cpp:
#include "Statistics.h"
using namespace std;
//find median value
double getMedian(int myArray[], int size){
int i, j, arrayTemp, *sortedArray = new int[size];
// Copy data to sortedArray
for (i = 0; i < size; i++) {
sortedArray[i] = myArray[i];
}
// Sort using bubble sort
for(i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (sortedArray[j] > sortedArray[j+1]) {
arrayTemp = sortedArray[j];
sortedArray[j] = sortedArray[j+1];
sortedArray[j+1] = arrayTemp;
}
}
}
// Middle or average of middle values in the sorted array.
double arrayMedian = 0.0;
if ((size % 2) == 0) {
arrayMedian = (sortedArray[size/2] + sortedArray[(size/2) - 1])/2.0;
}
else {
arrayMedian = sortedArray[((size +1)/2)-1]; // Or sortedArray[size / 2]
}
return arrayMedian;
}
//get largest value
int getLargest(int myArray[], int size)
{
int largest = myArray[0]; // Suppose first element is largest
for (int k = 1; k < size; k++) // Check for even more larger element
{
if( myArray[k] > largest) // If found even more larger
largest = myArray[k]; // Update largest
}
return largest;
}
Q2.cpp:
#include "FileFunctions.cpp"
#include "Statistics.cpp"
#include <iostream>
using namespace std;
// write the array forward and then backwards: [1 2 3 4 5 5 4 3 2 1]
int * DoubleandReverse (int *list, int size)
{
int *newList = new int[size * 2]; // double sized new list
int i;
for(i = 0; i < size; i++)
{
// Copying into both locations
newList[2 * size - 1 - i] = newList[i] = list[i];
}
return newList;
}
int main()
{
char name[50];
int i, *myArray, arraysize, largest, *doubleArray;
double median;
cout<< "Please enter filename:";
cin >> name;
WriteRandomData(5, 5555, name); // Create data.txt file
ReadData(name, arraysize, myArray); // Reading file
doubleArray = DoubleandReverse(myArray, arraysize); // Double size array
median = getMedian(myArray, arraysize); // median
largest = getLargest(myArray, arraysize); // get largest
// Display
cout << "Writing file: "<< name << endl;
cout << "Reading file: "<< name << endl;
cout << "Array size: "<< arraysize << endl;
cout << "Doubled and Reversed is [ ";
for(i = 0; i < 2 * arraysize; i++)
cout<<doubleArray[i] << " ";
cout<<" ] ";
cout << "Median is: " << median << endl;
cout << "Largest is: " << largest << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.