Honest HW attempt Failed. Please help!!

so i am learning files in class. i was to open a file manipulate the content and create a new file with the manipulate content. spent days on this program and when i run it it creates the new file but with the same content so i end up with two identical files. can anyone spot were i went wrong?

#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;

void recieved(int MyWaves[], int);
void created(int norm2[], int);
void norm1(int waves1[], int);
void comp1(int max[], int);
void newnorm(int comp2[], int);

int main(){
int threshold = 25000;
double gain = 0.8;
const int size = 100;
int MyWaves[size];
recieved(MyWaves, size);
}
void recieved(int waves1[], const int size){
int w = 100;
ifstream infile("input.txt");
if(infile.fail()){
cout << "Cannot open file" << endl;
system("pause");
exit(1);
}
for(int n = 0; n < size; n++){
infile >> waves1[n];
}
norm1(waves1, size);
}
void norm1(int max[], const int size){
int *mx = max;
int maxwave = 0;
int delta;
int wave;
for(int k = 0; k < size; k++){
if(mx[k] > maxwave){
maxwave = mx[k];
wave = k;
}
}
delta = 32767 - maxwave;
maxwave = maxwave + delta;
mx[wave] = maxwave;
comp1(mx, size);
}
void comp1(int comp2[], const int size){
int *c2 = comp2;
int threshold = 25000;
double gain = 0.8;
for(int j = 0; j < size; j++){
if(abs(c2[j]) > threshold){
c2[j] = c2[j] * gain;
}
}
newnorm(c2, size);
}
void newnorm(int norm2[], const int size){
int *n2 = norm2;
int maxwave = 0;
int delta;
int wave;
for(int k = 0; k < size; k++){
if(n2[k] > maxwave){
maxwave = n2[k];
wave = k;
}
}
delta = 32767 - maxwave;
maxwave = maxwave + delta;
n2[wave] = maxwave;
created(norm2, size);
}
void created(int newwaves[], const int size){
ofstream outfile("output.txt");
if(outfile.fail()){
cout << "Cannot create file" << endl;
system("pause");
exit(1);
}
else{
cout << "File created" << endl;
}
for(int n = 0; n < size; n++){
outfile << newwaves[n] << "\n";
}
outfile.close();
system("pause");
return;
}
The program appears reasonably valid. Either the particular "manipulations" done have no effect, or the data in the input file is not suitable. It's possible that a different input file would give a more interesting result.
so i tried it using simpler numbers and i noticed that a change was happening but only on the tenth number and the other 99 remained the same. i miss the good old days of physics and calculus when things made sense to me=/
Actually, when I tested it, only the last number changed for me too. As it happens I changed size to equal 10 before I ran the program.

Perhaps we both chose unsuitable data. Or the algorithm, though seemingly capable of changing the data, fails to do so.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void norm1(int max[], const int size){
    int *mx = max;
    int maxwave = 0;
    int delta;
    int wave;
    for(int k = 0; k < size; k++){
        if(mx[k] > maxwave){
            maxwave = mx[k];
            wave = k;
        }
    }
    delta = 32767 - maxwave;
    maxwave = maxwave + delta;
    mx[wave] = maxwave;
    comp1(mx, size);
}


This finds the largest number in the array and changes it. If your numbers are sorted, that's likely to be the last element of the array. That's all it changes -- 1 value.

1
2
3
4
5
6
7
8
9
10
11
void comp1(int comp2[], const int size){
    int *c2 = comp2;
    int threshold = 25000;
    double gain = 0.8;
    for(int j = 0; j < size; j++){
        if(abs(c2[j]) > threshold){
            c2[j] = c2[j] * gain;
        }
    }
    newnorm(c2, size);
}


This looks for values over 25000 in the array. It ignores all other values, so if the numbers in your file are 25000 or under, they won't be modified here - remember only one value has been adjusted when this function is entered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void newnorm(int norm2[], const int size){
    int *n2 = norm2;
    int maxwave = 0;
    int delta;
    int wave;
    for(int k = 0; k < size; k++){
        if(n2[k] > maxwave){
            maxwave = n2[k];
            wave = k;
        }
    }
    delta = 32767 - maxwave;
    maxwave = maxwave + delta;
    n2[wave] = maxwave;
    created(norm2, size);
}


This looks for the maximum number in the array again and changes it. If there weren't any numbers in the file greater than 25000, I'd say there's a good chance it will modify the same value it did previously.
deleted
Last edited on
Change the logic to do what you want?
#include <iostream>

#include <fstream>

#include <cmath>

#include <string>

using namespace std;

// Constants

const int DATA_SIZE = 100;

const int DATA_MAX = 32767;

const int THRESHOLD = 25000;

const float GAIN_FACTOR = 0.8;

// prototypes

void read_data(int data[DATA_SIZE]);

void normalize_data(int data[DATA_SIZE]);

void compress_data(int data[DATA_SIZE]);

void write_data(int data[DATA_SIZE]);

int find_max(int data[DATA_SIZE]);

// Main program entry point

int main()

{

int input_data[DATA_SIZE];

read_data(input_data);

normalize_data(input_data);

compress_data(input_data);

normalize_data(input_data);

write_data(input_data);



return 0;

}

// Read data into an array from an text file

void read_data(int data[DATA_SIZE]){

ifstream input_stream("input.txt");

if(input_stream.fail()){

cout << "ERROR: Cannot open file!" << endl;

exit(1);

}

for(int n = 0; n < DATA_SIZE; n++){

input_stream >> *(data + n);

}



}

// Normalize all the data in an array to +- DATA_MAX

void normalize_data(int data[DATA_SIZE]){

int max = find_max(data);

double factor = (double)DATA_MAX / max;

cout << "Normalizing constant = " << factor << endl;

for(int n = 0; n < DATA_SIZE; n++){

*(data + n) = static_cast<int>(*(data + n) * factor);

}

}

// Simple compression of high data points in an array

void compress_data(int data[DATA_SIZE]){

for(int n = 0; n < DATA_SIZE; n++){

if(abs(*(data + n)) > THRESHOLD){

*(data + n) = static_cast<int>(*(data + n) * GAIN_FACTOR);

}

}

}

// Write an array to an output file

void write_data(int data[DATA_SIZE]){

ofstream output_stream("output.txt");

if(output_stream.fail()){

cout << "Error creating output file" << endl;

exit(1);

}

for(int n = 0; n < DATA_SIZE; n++){

output_stream << *(data + n) << endl;

}

}

// Find the absolute maximum value (+ or -) and return

// the abs value of that value (we want the multiplier to be positive)

int find_max(int data[DATA_SIZE]){

int max = 0;

for(int n = 0; n < DATA_SIZE; n++){

if( abs(*(data + n)) > max){

max = *(data + n);

}

}

return abs(max);

}
Teacher posted a possible solution.
Teacher posted a possible solution.

The problem I had with this question was not the solution. It was the fact that the question was not supplied. That's why no-one could really help.
My apologies. I am new to this website and I did not want to come off as just trying to get my homework done for me. Still thanks for your time and consideration in my question.
Topic archived. No new replies allowed.