dimanche 10 octobre 2021

How can I move these functions from main.cpp to functions.cpp? [duplicate]

I currently am having an issue completing this program (the program is supposed to dynamically create two integers that are parallel and then randomly fill them, display them, add them, and get their sum and average), and have all the working code I just am trying to separate it from one main.cpp file into a main.cpp that calls functions from a function.cpp. Here is what I have so far:

main.h

#ifndef MAIN_H
#define MAIN_H

#include <iostream>
#include <ctime>
#include <cstring>
#include "functions.h"

using std::cout;
using std::cin;
using std::endl;
using std::fstream;
using std::string;

#endif /* MAIN_H */

functions.h

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include<iostream>
#include<ctime>
#include<cstring>
#include<string>

#define MIN 1
#define MAX 100

int sum_array(int*, const int);
float avg_array(int*, const int);
void add_arrays(int*, int*, int*, const int);
void display_array(int*, const int);
void fill_random(int*, const int);
bool is_positive_integer(char*);

#endif /* FUNCTIONS_H */

functions.cpp


#include "functions.h"

    /*
     * write your function definitions here to create the program as
     * specified in the assignment and shown in the examples
     */

main.cpp

#include<iostream>
#include<ctime>
#include<cstring>
#include<string>
#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#define MIN 1
#define MAX 100
using namespace std;
// declare all the functions
int sum_array(int*, const int);
float avg_array(int*, const int);
void add_arrays(int*, int*, int*, const int);
void display_array(int*, const int);
void fill_random(int*, const int);
bool is_positive_integer(char*);

#endif
//int argc, char** argv
int main() 
{
        char *argv = new char[20];
        cout<<"$ a.out ";
        cin>>argv;
        int n;
        // check if string is a positive integer greater thanz zero
        if(is_positive_integer(argv) == 1)      
        {
                // if yes then convert it to integer
                n = atoi(argv);
        }
        // if not the print error message and terminate program
        else
        {
                cout<<"usage: a.out n (where n is a number above 0)\n";
                cout<<"\n";
                
                return 0;
        }
        // declare three dynamic arrays of size n
        int *numbers1 = new int[n];
        int *numbers2 = new int[n];
        int *answer = new int[n];
        srand(time(NULL));
        // fill two arrays with random numbers
        fill_random(numbers1, n);
        fill_random(numbers2, n);
        // display both arrays
        cout<<"the two arrays created are:\n";
        display_array(numbers1, n);
        display_array(numbers2, n);
        // add both ararys and store sum into answer array
        cout<<"\nadding the arrays together:\n";
        add_arrays(answer, numbers1, numbers2, n);
        // display answer array
        display_array(answer, n);
        // diplay the sum of both arrays
        cout<<"The sum and average of the arrays:";
        cout<<"\narray 1 sum: "<<sum_array(numbers1, n);
        cout<<"\narray 1 average: "<<avg_array(numbers1, n);
        // diplay the sum of both arrays
        cout<<"\n\narray 2 sum: "<<sum_array(numbers2, n);
        cout<<"\narray 2 average: "<<avg_array(numbers2, n);
        // delete all three arrays and free the space used by them
        delete(numbers1);
        delete(numbers2);
        delete(answer);
        return 0;
}
// function to find the sum of array
int sum_array(int *numbers, const int n)
{
        // to store sum of array elements
        int sum = 0;
        // traverse the array using for loop
        for(int i=0; i<n; i++)
                sum += numbers[i];
        return sum;
}
float avg_array(int *numbers, const int n)
{
        // find sum of array
        int sum = sum_array(numbers, n);
        // divide sum by n to find average
        float avg = (float) sum / n;
        // return average
        return avg;
}
void add_arrays(int *answer, int *numbers1, int *numbers2, const int n)
{
        // traverse the array using for loop to add element at index i
        for(int i=0; i<n; i++)
                answer[i] = numbers1[i] + numbers2[i];
}
void display_array(int *numbers, const int n)
{
        // traverse the array using for loop
        cout<<"[";
        for(int i=0; i<n; i++)
                cout<<numbers[i]<<" ";
       cout<<"]";
        cout<<endl;
}
void fill_random(int *numbers, const int n)
{
        // traverse the array using for loop
        for(int i=0; i<n; i++)
        {
                // generate a random number between 1 and 100
                numbers[i] = (rand() % MAX) + MIN;
        }
}
// function to check if passed string is positive integer number
bool is_positive_integer(char *s)
{
        // check if string is not a number
        if(atoi(s) == 0)
        {
                return false;
        }
        // if string is a number
        else
        {
                // then check if number is positive
                if(atoi(s) > 0)
                        return true;
                // if number is negative
                else
                        return false;
        }
}

I also am trying to turn the "using namespace std" into this format.

using std::cout;
using std::cin;
using std::endl;
using std::fstream;
using std::string;

Thank you if anyone can help I really appreciate any guidance!

Aucun commentaire:

Enregistrer un commentaire