program using pointers

I was asked to do this assignment by my teacher. However i was absent for 3 days so i missed his lecture, would you help me just to get started with this program. Thank you for your help.

The primary purpose of this program is exercising your knowledge of complex function calls and
nested looping statements. You are to read in a series of numbers into an array of fixed length not to
exceed 100 numbers. Your program will stop accepting input when a negative number is input. Once
the negative number is input, your program will sort the numbers using a unique sorting algorithm
called the Hamming Sort and output the sorted array and the average (mean).


Programming Specifications:

 Your program will prompt the user to enter in from 2 to 100 positive integers.
 Input will be stopped when a negative number is entered.
 Your program will store the numbers in an array and sort them using the Hamming Sort
algorithm.
 You must not use Global Variables and pass values as parameters.
 The output will include the array sorted in ascending order with the average printed out at the
end.
Last edited on
If you start doing something, we will tell you what's wrong with it. But if you show no work no one will help you. People want to see at least some effort man. You can do it!
Just some skeleton
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
using namespace std;

void hamming_sort(int* numbers, int size)
{
	//your hamming sorting algorithm here
}

int get_input(int* numbers)  
{
	int size = 0;         //counts numbers entered

	//while correct data are entered and numbers entered are less than or equal to 100
        // ++numbers means go to the next element in an array
	for (int num; cin >> num && size <= 100 ; ++size, ++numbers)
	{
		if (num < 0) break; //exit loop if negative number is entered

		numbers[size] = num; //else write number you got into the array
	}

	return size; //return number of integers entered
}



int main()
{
	int number_array[100]; //create build in array for 100 integers

    //build in array implicitly converts to pointer to its 1st element
	int size = get_input(number_array);  

	//sort array
	hamming_sort(number_array, size); 

	//print out array

	//calculate the mean and print it out

	//and the most important part :D
	return 0;
}
Last edited on
thank you so much etrusks . That helps alot. I get the general idea. I really appreciate it.
I'm sorry I made a mistake in an input function, this skeleton is better
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* Your program will prompt the user to enter in from 2 to 100 positive integers.
 Input will be stopped when a negative number is entered.
 Your program will store the numbers in an array and sort them using the Hamming Sort
algorithm.
 You must not use Global Variables and pass values as parameters.
 The output will include the array sorted in ascending order with the average printed out at the
end.*/

#include <iostream>
using namespace std;

void hamming_sort(int* numbers, int size)
{
	//your hamming sorting algorithm here
}

int get_input(int* numbers)  
{
	int size = 0;         //counts numbers entered
	cout << "Enter numbers : ";
	//while correct data are entered and numbers entered are less than 100 (0 ... 99)
	for (int num; cin >> num && size < 100 ; ++size)
	{
		if (num < 0) break; //exit loop if negative number is entered

		numbers[size] = num; //else write number you got into the array
	}

	return size; //return number of integers entered
}

void print_array(int* numbers, int size)
{
	cout << "{ ";
	for (int i = 0; i < size; ++i)
	{
		cout << numbers[i];
		if (i + 1 < size) cout << ", ";
	}
	cout << " }\n";
}


int main()
{
	int number_array[100]; //create build in array for 100 integers

    //build in array implicitly converts to pointer to its 1st element
	int size = get_input(number_array);  

	//sort array
	hamming_sort(number_array, size); 

	//print out array
	print_array(number_array, size);
	//calculate the mean

	//and the most important part :D
	return 0;
}
Last edited on
thank you so much! Just wondering if i am doing the hamming sort algorithm correctly. i am guessing not .

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;

void hamming_sort(int* numbers, int size)
{
	 int numb[10];
	int i, j;

 for(i=0;i<=9;i++)
 {
  
  cin >> numb[i];
 }

 for(i=0;i<=9;i++)
 {
  for(j=i+1;j<=9;j++)
  {
   int temp;

   if(numb[i] > numb[j])
   {
    temp = numb[i];
    numb[i] = numb[j];
    numb[j] = temp;
   }
 
  }
  
 }
 cout<<"sorted list"<<endl;

 for(i=0;i<=9;i++)
 { 
	 
  cout << endl <<  numb[i] << endl;
 }

int get_input(int* numbers)  
{
	int size = 0;         //counts numbers entered
	cout << "Enter numbers : ";

	while(size>=0 || size <=100)
	{
	for (int num; cin >> num && size < 100 ; ++size)
	{
		if (num < 0) break; //exit loop if negative number is entered

		else 
			numbers[size] = num; //else write number you got into the array
	}

	return size; //return number of integers entered
}
}

void print_array(int* numbers, int size)
{
	cout << "{ ";
	for (int i = 0; i < size; ++i)
	{
		cout << numbers[i];
		if (i + 1 < size) cout << ", ";
	}
	cout << " }\n";
}


int main()
{
	int number_array[100]; //create build in array for 100 integers

    //build in array implicitly converts to pointer to its 1st element
	int size = get_input(number_array);  

	//sort array
	hamming_sort(number_array, size); 

	//print out array
	print_array(number_array, size);
	//calculate the mean

	//and the most important part :D
	return 0;
}
Last edited on
Topic archived. No new replies allowed.