-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
165 lines (135 loc) · 4.23 KB
/
main.c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Project 59 -
Description:
Make an algorithm in C language that receives as input a matrix
of characters. Each position in the array must store a character;
The matrix dimension should be MxN, where M and N are chosen input data
by the user. Declare the matrix as having a maximum size of 10x10, and validate the values
M and N, which must be between 1 and 10, including 1 and 10. For validation, the user must
get stuck in the program until you enter a valid number for M and N;
After choosing the dimension of the matrix and populating it with characters:
1) Count the number of occurrences of each character that appears in the matrix.
After counting, list each character entered and the number of occurrences;
2) Create a function that receives the first registered character as a parameter
in the matrix. The function must return a numeric data. If the character is
uppercase, return the result of this character divided by 10 in the function.
Otherwise, return the result of the character multiplied by 2;
Here is the output test:
***************************************************************
Output:
Please, enter the matrix dimension (number): 2
Thanks! Your matrix wil be mtx[2][2]:
Now populate the mtx[2][2].
Enter letter for index:
mtx[0][0]: G
mtx[0][1]: i
mtx[1][0]: l
mtx[1][1]: l
Printing the matrix in the screen:
G i
l l
The frequencies of characters in a matrix are:
G/g occurs 1 times.
I/i occurs 1 times.
L/l occurs 2 times.
Math Operation:
G is the First Character entered in the matrix and its numeric value is 71;
if we take 71 and divide by 10 we get 7.
***************************************************************
Based: Projects 49, 51 & 58 :)
Edited by J3
Date: Jul, 20/2020
*/
#include <stdio.h>
#include<stdlib.h>
#include<string.h>
int ROW;
int COLUMN;
int b, c;
int i, j;
char mtx[10][10];
int size = 0;
int c = 0, count[26] = {0}, x, sum;
/* prototype function */
int getFirstChar(int c);
int main()
{
/* Ask for the Matrix dimension - Entering a number between 1 & 10 */
printf("Please, enter the matrix dimension (number): ");
scanf("%d", &size);
while(size > 10 || size == 0 )
{
while((c = getchar()) != '\n' && c != EOF){};
printf("Please, the matrix size must be in the range of 1-10\n:");
scanf("%d", &size);
}
ROW = size;
COLUMN = size;
printf("Thanks! Your matrix wil be mtx[%d][%d]:\n", size, size);
printf("Now populate the mtx[%d][%d].\nEnter letter for index:\n", size, size);
for(i = 0; i < ROW; i ++)
{
for(j = 0; j < COLUMN; j++)
{
mtx[size][size];
while((c = getchar()) != '\n' && c != EOF){};
printf("mtx[%d][%d]: ", i, j);
scanf_s("%c", &mtx[i][j]);
}
}
// Displaying the Matrix in the screen
printf("\nPrinting the matrix in the screen:\n");
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COLUMN; j++)
{
printf("%c\t", mtx[i][j]);
}
printf("\n");
}
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COLUMN; j++)
{
/* Considering characters from 'a' to 'z' and 'A' to 'Z' */
if (mtx[i][j] >= 'a' && mtx[i][j] <= 'z')
{
x = mtx[i][j] - 'a';
count[x]++;
} else
if (mtx[i][j] >= 'A' && mtx[i][j] <= 'Z')
{
x = mtx[i][j] - 'A';
count[x]++;
}
}
}
/** Calculating the frequency table */
printf("\nThe frequencies of characters in a matrix are:\n");
for (c = 0; c < 26; c++)
if(count[c] != 0)
{
printf("%c/%c occurs %d times.\n", c + 'A',c + 'a', count[c]);
}
/* Testing requirement 2 above */
printf("\nMath Operation:\n");
printf("%c is the First Character entered in the matrix and its numeric value is %d;\n", mtx[0][0], mtx[0][0]);
getFirstChar(mtx[0][0]);
return 0;
}
/* Function to addressing requirement 2 above */
int getFirstChar(int c)
{
if( isupper(c))
{
int r = c / 10;
printf("if we take %d and divide by 10 we get %d.", c, r);
return r;
}
else
{
int r = c * 2;
printf("if we take %d and multiply by 2 we get %d.", c, r);
return r;
}
}