|
This Post has some Previous Posts, you can access the first Main Post here: Some C++ Problems for Beginners (1)
The code was written and tested on Visual Studio 2010 –> “Empty Windows Console Application” . if you don’t have Visual Studio, you can get the latest version from :
-Visual C++ 2010 Express:
http://www.microsoft.com/visualstudio/eng/downloads#d-2010-express
note that, you can also use another IDEs like:
-Visual Studio Express 2012 for Windows Desktop: http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products#product-express-summary
-CodeBlocks: http://sourceforge.net/projects/codeblocks
-GNU C++ compiler GCC(g++): http://gcc.gnu.org/
-MingW GCC port for windows: http://www.mingw.org/
-C++ Builder (Borland): http://www.embarcadero.com/products/cbuilder
-2DimArrayFrom1DimArray.cpp:
Problem Definition:
Supposing that you have the following 1 Dimensional Array called A as a known input:
int A[10] = {1,1,2,5,3,4,9,8,7,1};
Fill the Char Array B (char B [10][10]) with the Histogram values taken from the Array A
then Print the Histogram in B
================================================
output:
-------
*
*
**
*****
***
****
*********
********
*******
*
================================================
|
[C++]
1 /* About The Simple Code "_2DimArrayFrom1DimArray.cpp"
2 Copyright (C) 2013 Eng. Mohamed Adel (http://www.med-adel.com)
3
4 This simple code is a free simple: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.*/
16
17 #include <iostream>
18 using namespace std;
19
20 /*Problem Definition
21 Supposing that you have the following 1 Dimensional Array called A as a known input:
22 int A[10] = {1,1,2,5,3,4,9,8,7,1};
23
24 Fill the Char Array B (char B [10][10]) with the Histogram values taken
25 from the Array A then Print the Histogram in B
26 ================================================
27 output:
28 -------
29
30 *
31 *
32 **
33 *****
34 ***
35 ****
36 *********
37 ********
38 *******
39 *
40
41 ================================================
42 //*///Prototypes
43 void FillArray(int A[10], char B[10][10]);
44 void PrintArray(char B[10][10]);
45
46 void main()
47 {
48 int A[10] = {1,1,2,5,3,4,9,8,7,1};
49 char B [10][10];
50
51 FillArray(A, B);
52 PrintArray(B);
53 }
54
55 //Function "FillArray" takes 2 Attributes A --> The input Array & B --> The output Array
56 //It takes the values from the Array "A" and fill the Output Array "B" with
57 //Stars and Spaces to Draw the Histogram
58 void FillArray(int A[10], char B[10][10] )
59 {
60 for(int i = 0; i < 10; i++)
61 {
62 for(int j = 0; j <10; j++)
63 {
64 if( j < A[i])
65 {
66 B[i][j] = '*';
67 }
68 else
69 {
70 B[i][j] = ' ';
71 }
72 }
73 }
74
75 }
76
77
78 //Function "PrintArray" takes 1 Attribute B --> The Output Array and prints it
79 //It Loops over all the Array "B" elements and print them all (The Histogram)
80 void PrintArray(char B[10][10])
81 {
82 for(int i = 0; i < 10; i++)
83 {
84 for(int j = 0; j < 10; j++)
85 {
86 cout<<B[i][j];
87 }
88 cout<<endl;
89
90 }
91
92 }
| |
download the 2DimArrayFrom1DimArray.cpp (Commented, Explained Code)
| |
No comments:
Post a Comment