![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Posts: 1
Rep Power: 0
![]() |
array issue
2 questions first did the main daniweb site get hacked or something...2nd can someone take a look at my code and see what I am doing wrong...can't seem to pinpoint it
Not sure what I am doing wrong but have an issue with a program...i am to input a file name that i have in my project and it is not doing it right... This is what my program should look like for each textfile... Sample Output 1 Enter the file to read in: data1.txt The numbers in the array are: 45.3 57.4 23.6 34.2 234.6 34.9 54.8 934.9 8432.5 9809.3 539.7 43.9 12.0 The numbers in the array are: 12.0 23.6 34.2 34.9 43.9 45.3 54.8 57.4 234.6 539.7 934.9 8432.5 9809.3 The average of the numbers is 1558.2 The median of the numbers is 54.8 Press any key to continue What I am getting is... Enter the file to read in: data1.txt The numbers in the array are: The average of the numbers is 45 The median of the numbers is 0 Press any key to continue... Not sure what I am doing wrong any help would mean alot c++ Syntax (Toggle Plain Text)
|
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jan 2008
Posts: 2
Rep Power: 0
![]() |
Re: array issue
Here's something that does pretty much the same thing. I wrote it earlier this semester for my principle of programming languages class:
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <exception>
using namespace std;
// Parses the input file into the array's size and contents.
// Dynamically allocates memory for the array, fills it, and returns.
void parse_file(char * filename, double *&a, int &size) {
ifstream myfile;
myfile.exceptions( fstream::failbit );
// Get the file name, and try to open the said file
printf("Input the filename: ");
scanf("%s",filename);
// Try opening the file, if it doesn't exist, throw an exception
try {
myfile.open(filename);
} catch(ifstream::failure e) {
printf("File does not exist or is unreadable\n");
}
// Read the first thing from the file as an int and store it as 'num'
myfile>>size;
// allocate an array to the exact that we need
a = (double *) malloc(size*sizeof(double));
// Fill the array
for(int i=0; i<size; i++) {
myfile>>a[i];
}
myfile.close();
}
// Standard way of calculating average
// Sums the elements of a, puts them into total, divides by size and returns.
double find_average(double *&a, int size) {
double total = 0;
for(int i=0; i<size; i++) {
total = total + a[i];
}
return (total/(double)size);
}
// Standard way of calculating standard deviation
// Sums the square of each element minus the average
// Divides the total of squares by the size and returns the positive square root of the sum.
double find_standard_dev(double *&a, int size, double average) {
double dev = 0;
for(int i=0; i<size; i++) {
dev = dev + pow(a[i]-average,2);
}
dev = dev/size;
return (sqrt(fabs(dev)));
}
int main() {
// Set up some variables
char filename[50]; // 50 characters for the filename should be good enough unless the path is included.
double average = 0, standard_dev = 0; // Zero out our variables before we start using them
int num; // Array size
double * array; // Make a pointer to our array
parse_file(filename, array, num);
average = find_average(array,num);
standard_dev = find_standard_dev(array, num, average);
printf("Average: %lf\n",average);
printf("Standard Deviation: %lf\n",standard_dev);
free(array);
return 0;
} |
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 799
Rep Power: 3
![]() |
Re: array issue
In your Fill_Array function, you don't set the number_used variable to the number of items found.
In your print_array function: void Print_Array(int Size[], int count)
{
int number_used = 0;
for(int index = 0; index < number_used; index++)
cout << Size[index] << " ";
}The Calc_Average and Calc_Median functions need to be passed the number of items in the array, rather than using MAX_SIZE Also, your array is of type int, and the input has floating point numbers, you should look into using type double. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| dynamic array help | quickster12 | C++ | 4 | Nov 29th, 2007 11:52 PM |
| problem processing file into a char array | csrocker101 | C++ | 1 | May 8th, 2007 11:50 PM |
| changing size of an array | Eric the Red | Java | 3 | Apr 3rd, 2006 8:19 PM |
| Installing IPB 2.03 | bh4575 | Other Web Development Languages | 0 | Apr 23rd, 2005 2:36 AM |
| Converting 1-dimensional array to 2-dimensional array | Tazz_Mission_13 | Java | 6 | Apr 8th, 2005 11:58 AM |