Using pointers and Dynamic Arrays, Confused

closed account (LEwpfSEw)
I have a current assignment I'm trying to finish by tomorrow but I'm totally clueless as to how I can fully "build" this program.

I'm currently working with Pointers and Dynamic Arrays and I don't have the slightest clue on what they are. I've tried looking online for definitions but none of them were given in simple terms.

I'm supposed to create a program that creates 10 random numbers and then print them out. After printing them out, I have to sort them in ascending order using bubble sort. After sorting I'm supposed to print out the sort and be done with the program.

I've split it into three files (Header file, Implementation file, and Main)

The problem is I really don't know how to fully build each file and then connect them together.

My Header File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef Asst3
#define Asst3






class Arraysort
{
private: 
	int * array ;                  // pointer for array
	int size    ;
	bool sorted ;                  // unset in Constructor, set in sort_array() 

public:
	bool is_sorted(void)   ;       // getter for sorted
	int  get_size(void)    ;       // getter for size
	void load_array(int n) ;       // load with random numbers mod n
	void print_array(void) ;
	void sort_array(void)  ;       // bubble sort
	Arraysort(int n )      ;       // Constructor
	~Arraysort(void)       ;       // Destructor
} ;


#end if 


Implementation file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream> 
#include "Asst3.h"

void load_array  (int n)

{   int rand (void) ;
    
    
    for ( x = 0 ; x < SIZE ; x++)
        array[x] = rand() ;

    cout << "Random Numbers:\n" << rand ;

}


int whereSmallest(float A[],int begin,int end)
{
  float min = A[begin];
  int where = begin;
  int n;
  for(n = begin + 1;n < end;n++)
    if(A[n] < min) {
      min = A[n];
      where = n;
      }
  return where;
}


and my Main File:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <stdlib.h>
#include "Asst3.h"



int main ()

{
    
int array ;



float * D ;

A = (float*) malloc(count*sizeof(float))

Free (D) ;

return 0 ;



}


I know I'm missing a lot from each one, but that's why I'm coming on here for help. Thank you in advance!
1
2
3
4
5
float * D ;

A = (float*) malloc(count*sizeof(float))

Free (D) ;


Whats A ? And in c++ use new not malloc.
like d = new float[5];

I'm currently working with Pointers and Dynamic Arrays and I don't have the slightest clue on what they are.

When you are creating array you must know its size:
int array[size];
where size must be value, known before compilation.
But what if you don't know how many elements will me generated, or written by user? Then you create a pointer
int *pointer;
And when program is running you can allocate memory by using new and assign returned pointer to *pointer.
pointer = new int[size];
in this case size doesnt has to be constant\

Ps. http://www.cplusplus.com/doc/tutorial/pointers/
Last edited on
closed account (LEwpfSEw)
I'm sorry. I really do not know what A would be and how to use new in the Main File. I'm really new to C++ and I'm trying my best to understand all of it. You might have to dumb it down for me.

I've been trying to go through numerous online resources and trying to look at their code to change and implement into my own. It seems like I created more of a mess than a beginning.
Last edited on
Don't be sorry, because it won't help you. You are trying to rush through it too much.. start some c++ tutorial and do all exercises.

Programming is really simple with good approach:
I'm supposed to create a program that creates 10 random numbers and then print them out. After printing them out, I have to sort them in ascending order using bubble sort. After sorting I'm supposed to print out the sort and be done with the program.

1. creates 10 random numbers and then print them out
2. sort them in ascending order using bubble sort
3. print out the sort

Was it supposed to be done in c++ or c?
Once known, sit for an hr, read new and delete function, rand function and bubble sort, then you can write ur program in a few minutes, code is not important, but concepts are!!!
Last edited on
closed account (LEwpfSEw)

Was it supposed to be done in c++ or c?


I'm trying to write it as a C++ program in code::blocks 10.05

To anirudh sn:

I'm also currently watching introduction to C++ videos online to grasp the foundation of the programming. I have the purpose of the program down, but use of each code and what other codes that are out there, are the things that really tears me to pieces.

So far, I know that the header file declares/defines the objects I'm using (right?), and the implementation file holds the back ground functions that use the objects from my header file. Lastly, the main file is where I put them all together and give it the final organization.

I'll do as anriudh said and re-look at the codes I'm using.





Last edited on
I would be pissed if I were your teacher and I gave the instructions: "create a program that creates 10 random numbers and then print them out. After printing them out, sort them in ascending order using bubble sort. After sorting print out the array and be done with the program." and you gave me something with classes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// main.cpp

#include <iostream>

void makeRandom(int* array, const int size);
void printArray(int* array, const int size);
void sortArray(int* array, const int size);

int main(void)
{
  const int SIZE = 10;
  int *myArray = new int[SIZE];
  makeRandome(myArray, SIZE);
  printArray(myArray, SIZE);
  sortArray(myArray, SIZE);
  printArray(myArray, SIZE);

  delete [] myArray;
  return 0;
};

// Write the functions and you are done 




closed account (LEwpfSEw)
Thank you for your help, Lowest One, but the header with the class file was given by my teacher. That code you see is directly stated by him. I was allowed to separate the program into numerous file or compact them all into one.

I wish to apologize that I did not include that earlier in my first post.
Topic archived. No new replies allowed.