Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!

Notices

Welcome to LinuxQuestions.org, a friendly and active Linux Community.

You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!

Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.

If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.

Having a problem logging in? Please visit this page to clear all LQ-related cookies.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

Click Here to receive this Complete Guide absolutely free.

Im really not sure what to do here. I have tried to figure it out but the same message keeps coming up.

CODE:

#include <stdlib.h>
#include <math.h>

int main ()
{ /* main */
/*
***********************
* Declaration Section *
***********************
*/
/*
*************
* Constants *
*************
*/
/* This is the minumum value that can be input. */
const int min_value = 1;
/* This is the program failure code. */
const int program_failure_code = -1;
/* This is the program success code. */
const int program_success_code = 0;
/* This is the initial sum. */
const int initial_sum = 0;
/* What needs to be subtracted from the array. */
const int last_term = 1;
/* This if for the harmonic mean. */
const int inverse = 1;
/*
*************
* Variables *
*************
*/
/* This is the independent variable. */
float* independent_variable = (float*)NULL;
/* This is the dependent variable. */
float* dependent_variable = (float*)NULL;
/* These are the variable names for each thing being calculated. */
float indep_arithmetic_mean, indep_root_mean_square, indep_harmonic_mean;
float dep_arithmetic_mean, dep_root_mean_square, dep_harmonic_mean;
float indep_sum, dep_sum, harmonic_indep_sum, harmonic_dep_sum;
float rms_indep_sum, rms_dep_sum;
/* This is the linear least squares regression. */
float linear_least_squares_regression;
/* This is the first value. */
float first_value;
/* This is the input value. */
int list_length;
/* This is the index. */
/* This is the index. */
int index;
/*
*****************
* Input Section *
*****************
*/
/* Gets the input from the file for the list length. */

scanf("%d", &list_length);

if ( list_length < min_value ) {
printf("ERROR: That is not a valid input.\n");
exit(program_failure_code);
} /* if ( value < min_value ) */

else if (list_length == 0) {
printf("ERROR: That is not a valid input.\n");
exit(program_failure_code);
} /* if(list_length == 0) */

/* This is allocating the independent variable. */

} /* for index */
indep_root_mean_square = sqrt(rms_indep_sum / list_length);

/* Root mean square for dependent variable. */

for (index = initial_sum; index < list_length; index ++) {

rms_dep_sum += (dependent_variable[index] * dependent_variable[index]);
} /* for index */
dep_root_mean_square = sqrt(rms_dep_sum / list_length);

/* Harmonic Mean for independent variable */
for (index = initial_sum; index < list_length; index ++) {
harmonic_indep_sum += (inverse / independent_variable[index]);
} /* for index */
indep_harmonic_mean = (list_length / harmonic_indep_sum);

/* Harmonic Mean for dependent variable */
for (index = initial_sum; index < list_length; index ++) {
harmonic_dep_sum += (inverse / dependent_variable[index]);
} /* for index */
dep_harmonic_mean = (list_length / harmonic_dep_sum);

/*
******************
* Output Section *
******************
printf("%d\n", list_length);
printf("%f indep arithmetic mean\n", indep_arithmetic_mean);
printf("%f dep arithmethic mean\n", dep_arithmetic_mean);
printf("%f indep root mean square\n", indep_root_mean_square);
printf("%f dep root mean square\n", dep_root_mean_square);
printf("%f indep harmonic mean\n", indep_harmonic_mean);
printf("%f dep harmonic mean\n", dep_harmonic_mean);

At some point your program accesses memory which it didn't allocate. It is most likely due to uninitialized variables when you access the elements in your dynamically allocated array. I was able to fix your program by doing nothing except initializing variables.

1) You have a declaration for "float first_value"; You don't initialize it (what it means is that first_value currently holds a **random** value). You then use this variable to loop over your array: for (index = first_value; index < (list_length - last_term); index ++) {....}

Your loop should start at element 0 and continue while index < list_length. The loop will exit as soon as index == list_length, so no need to loop until list_length - last_term.

2) You also declare lots of "sum" variables:
float indep_sum, dep_sum, harmonic_indep_sum, harmonic_dep_sum;

None of these sums is initialized to 0. This won't cause a crash but could potentially result in incorrect calculations.

3) Besides that you should also #include <stdio.h> (needed for scanf), and notice that you never return program_success_code in the end of your main() function. You also have "/*", which starts a comment block, without terminating it with "*/". Plus other small errors...

To avoid pitfalls, treat warnings as errors. Every warning for unused variables etc' could hint you of a real bug. You can also use -Wall on the command line when compiling.

The glibc memory errors is quite often related to memory allocated on the stack of the function, instead of being dynamically allocated by malloc and siblings. This allows for a wide range of "strange" errors that are quite difficult to trace.

Make sure that every variable has a known value when defined, and every pointer leads to a known memory location, before being used. Run the program via 'ddd' to debug it and to find out where it fails.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.