Saturday, June 20, 2015

[Programming book Chapter Six] array.

Now that your programming knowledge has increased a little. Now, then let some public service activities. We favorite math teacher at the school for the program will write the test results. The first temporary school, the temporary and the annual number of these three tests is 100. 50% of the annual examination, periodic examination of the second periodic testing of 25% and 25% of the final results are published. The three tests are then input the number of our programs. We will show you the final result. 87 program output. (But I total_marks = ft_marks / 4.0 + st_marks / 4.0 + final_marks / 2.0; writing by entering the total_marks = ft_marks / 4 + st_marks / 4 + final_marks / 2; then comes the output of 86. What bad or my computer? ) But we could not do any good as our favorite teacher. The total number of students in his class of forty. The program will run the shelter callisabara! But it's not just a job. We should, through a program to evaluate the final outcome. This is no easy task. We started writing a program that could: #include int main () { int ft_marks_1, st_marks_1, final_marks_1, ft_marks_2, st_marks_2, final_marks_2, ft_marks_3, st_marks_3, final_marks_3, Surely you understand, I'm going to do? Thus, if you write a program to tell me how many of the variables? 160. Behavior to reduce pain, and increase our pain is so does not make sense. But such programs often need to write our own. Not to worry! Almost all programming lyanguyejei array (Array), a wonderful thing. The same goes for many of the variables put together. Let the variable name, a name that is to be condemned array. C also has an array. Such variables have a data type, the array. The types of data to the array that can only hold the same datai. Char array of char type as the type of thing. There will be many things to say at the beginning of the array. int ara [10]; Thus, we can declare an array whose name ara, in which only the integer data type, and the array can hold a total number of ten. The first ara [0] (yes, ara [1] does not), the second ara [1], the third ara [2], the tenth of the number ara [9]. In other words, ara [i] is the i + 1 th element. Now let's play a little bit of the array. Each program will continue, but of course the computer. The key output should come in? I do not know and can not know it. Any number might come. These garbage (garbage) said. Because the array is actually 1, 5, 100, this index does not have anything to say. The array length will be 5, so the index from 0 to 4. If you want to show all the elements together in an array, now, what it means? The first element is the way (ara [0]), the second element (ara [1]), the third element (ara [2]) ... all the way to print one. And of course, we'll take a loop. The first element to the last element would show up? No problem, just loop over the index from 0 to 9 will analei. You are now entering a Fellow of the program. Then a small problem. There is an array of ten elements, they will be in reverse order. That would be the first material component of the tenth, the tenth to be the first, the ninth the second, the second chrysoprasus .. like this. What we can do for her, you can take another array. Ayaretite the array of ingredients will reverse the order. The second array will then copy the array. Notice here that the first element of the array array of brackets in case I did not say that, because the C compiler can understand looking at the numbers in the brackets are the elements that ara ten seconds. Ara2 on the second array contains nothing now. So to say at the outset that it will have many components. According to the compilers of the memory array will take place. The program is working well. But if you can understand a little, thinking that there was no need to use a second array. We could do it in a common system. int temp; temp = ara [9]; ara [9] = ara [0]; ara [0] = temp; The first and tenth components were swapped. Then temp = ara [8]; ara [8] = ara [1]; ara [1] = temp; The second and ninth components were swapped. The program for free. What do you see? Output What happens? 10 20 30 40 50 60 70 80 90 100 So did the job! I made a small mistake in fact, that you find out. Such a mistake to say that the bug (bug), the program runs properly, but not the output. There is a bug in my code, then debug (debug) or (meaning not just bug out). Now let's go back to our previous problems. We are now the first math test, the number of temporary lay in an array, the second array number of periodic testing, annual exams and results for a number of categories that will use an array. int ft_marks [40], st_marks [40], final_marks [40]; double total_marks [40]; Roll Number 1, which would have seen the first cell in the array (that is, to index 0). Now tell me total_marks [34] - The total number of cars do you have? 35 of the roll number. N roll number, if any, when the total number total_marks [n-1]. Runs on, how beautiful the output! I have a place on the printf function \ t written, the tabs (Tab) to be printed on (the keyboard on the left hand side). The roll printing the total marks with a tab to see if the print is a little better for the \ t have to use, otherwise there is no need. But you look so beautiful in the program where you do not feed the little catapati sat down opposite another Caprice. He'd like to see how many have a number. I mean, how many were 50, 51 ... how many of these things, and what they found. Before reading the rest of the program you try to write your own. Now if you do not wish to stop reading the book, and any time after that try. I hope some of you have written the program. If you do not enter at least one hour after the effort we can to try to solve. At the beginning of the 50's a concern that someone, but did not get the numbers down. So 50 out of 100 if it could be any number of how many. It is the first solution that comes to my mind first, we total_marks array, how many there are 50, and then see how many have 51 ... it will up to 100. This means that for all numbers from 50 to 100. The numbers array total_marks'll check. for (marks = 50; marks <= 100; marks ++) {loop marks- the value of the first 50, then 51, was one of the 100 up to stretch. count = 0; Public assuming zero 'marks' has a number. marks- of quality for all the work that we do this. Then take a look at total_marks array is equal to any number if marks-, but count- will increase the value of one. If a number (marks) are each array, count-'s value will be greater. for (i = 0; i <40; i ++) { if (total_marks [i] == marks) { count ++; } } printf ("Marks:% d Count:% d \ n", marks, count); Here we have the marks and the number of times (count) that would print it. } Is not difficult. The simple solution is to use nested for loop carried. Tell me that's the condition we're checking out of if- (total_marks [i] == marks) This work program is how many times? Outside the inner lupati pratibarera lupati turn 51 and turn 40 times. 51 x 40 = 2040 times the total. The above program, we'll try to write a little differently. I've been here for, have an additional array. marks_count an integer array type and marks_count [n], we understand that there are n number of times in total_marks-. As the number could be between 0 and 100. So we have to arrange the array to a total number of 101. int marks_count [101]; Since the beginning do not know anything, so assume that all of the zero bar. Set to 0 to give all cells in the array marks_count: for (i = 0; i <101; i ++) {marks_count [i] = 0; } Now total_marks array of standard cells for each number marks_count increase the value of the array. for (i = 0; i <40; i ++) {marks_count [total_marks [i]] ++; } The problem is that you? Think a little. I- of the value 0, then total_marks [i] being total_marks [0], ie 86. Now what we need is an array marks_count the house (meaning marks_count [86]) to increase the value one. At the beginning of zero, one, now. But the thing we've marks_count [total_marks [i]] - one of the values ​​we have extended marks_count [total_marks [i]] ++; That was actually written eibhabeo: t_m = total_marks [i]; marks_count [t_m] ++; They also run the following program on the computer is still the head culakaccha. Here each marks_count [total_marks [i]] ++; After marks_count one line array we've printed.

No comments:

Post a Comment