Arrays and for loops

Just a personal contribution to the open source code. Hope it helps.


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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
using namespace std;

int main(){

	int array[] = {4,2,3,1,5,6,7,11,9,10,8};	// 10 elements 1-10.
	int size = sizeof(array)/sizeof(int);

	cout << "Original Elements in order are: ";
	for (int i = 0; i < size; i++){
		cout << array[i] << " ";	// print elements.
	}

	cout << endl;
	cout << "And the size of the array is: "; 	// The number of elements.
	cout << size << endl;	// Formula!

	int sum = 0; 	// Declare variable and initialize to 0.
	for (int i = 0; i < size; i++){
		sum += array[i];
	}
	cout << "The addition of elements sums up to: " << sum << endl;

	int average = sum/size;		// sum = 66 and size = 11. To get average 66/11 = 6.
	cout << "The average number in the set is: " << average << endl;

	int max = 0;
	int min = 0;
	int startElement = 0;

	if(sum > 0 && size > 0){
		startElement = array[0];	// Value of array at index 0 is 1.
	}

	for (int i = 0; i < size; i++){
		if (startElement > array[i]){
			startElement = array[i];
		}
		min = startElement;
	}
	cout << "The minimum element of the array is: " << min << endl;



	for (int i = 0; i < size; i++){
		if (startElement < array[i]){
			startElement = array[i];
		}
		max = startElement;
	}
	cout << "The maximum element of the array is: " << max << endl;



    int temp = 0;

    // This situation is called nested for loop.
    for (int a = 0; a < size; a++){		
		for (int i = 0; i < (size-1); i++){
			if (array[i] > array[i+1]){
				temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
			}
		}
	}
	cout << "The sorted numbers from lowest to highest are: ";
	for (int i = 0; i < size; i++){
		cout << array[i] << " ";
	}

    for (int a = 0; a < size; a++){		
		for (int i = 0; i < (size-1); i++){
			if (array[i] < array[i+1]){ // Highest to lowest, just change > to<.
				temp = array[i];
				array[i] = array[i+1];
				array[i+1] = temp;
			}
		}
	}
	cout << endl;
	cout << "The sorted numbers from highest to lowest are: ";
	for (int i = 0; i < size; i++){
		cout << array[i] << " ";
	}

	// Linear sorting:
    cout << endl;
	cout << "Enter a number from the set {11,10,9,8,7,6,5,4,3,2,1} to search its position indvidually: ";
	int number;
	cin >> number;

	cout << endl;

	int position;
	for (int i = 0; i < size; i++){
		if (number == array[i]){
			position = i+1;		// index i is the position holding the number.
			break;	// breaks ends the loop after finding the element.
		}
	}
	cout << "The position of the element " << number << " is " << position << endl;


	return 0;
}
Last edited on


// Output:
Original Elements in order are: 4 2 3 1 5 6 7 11 9 10 8 
And the size of the array is: 11
The addition of elements sums up to: 66
The average number in the set is: 6
The minimum element of the array is: 1
The maximum element of the array is: 11
The sorted numbers from lowest to highest are: 1 2 3 4 5 6 7 8 9 10 11 
The sorted numbers from highest to lowest are: 11 10 9 8 7 6 5 4 3 2 1 
Enter a number from the set {11,10,9,8,7,6,5,4,3,2,1} to search its position: 5   // Assume 5

The position of the element 5 is 7
Last edited on
int average = sum/size; // sum = 66 and size = 11. To get average 66/11 = 6.

I am afraid that won't work if the answer is not a whole number, because of integer math. Use double instead.

break; // breaks ends the if statement automatically after finding the element.

break breaks loops, not if statements.

You should investigate using functions more :+)
Topic archived. No new replies allowed.