Answer:
f0ll0w me on insta gram Id:Anshi threddy_06 (no gap between I and t)
Answer:
- #include <stdio.h>
-
- bool IsArrayEven(int inputVals[], int numVals){
- int i;
- for(i = 1; i < numVals; i++){
- if(inputVals[i] % 2 != 0){
- return false;
- }
- }
- return true;
- }
-
- bool IsArrayOdd(int inputVals[], int numVals){
- int i;
- for(i = 1; i < numVals; i++){
- if(inputVals[i] % 2 == 0){
- return false;
- }
- }
- return true;
- }
-
- int main()
- {
- int my_input[] = {5, 2, 4, 6, 8, 10};
-
- if(IsArrayEven(my_input)){
- printf("all even");
- }else if(IsArrayOdd(my_input)){
- printf("all odd");
- }else{
- print("not even or odd");
- }
- }
Explanation:
Firstly, write two functions to check if the input array is all even or all odd (Line 3-22). The key idea is to use modulus operator to check if a number % 2 equal to 0, it is an even number, if not, it is an odd. So long as one of the number is odd in the isArrayEven function, the function shall return false (Line 7). If no odd value detected, it return true (Line 11). The similar code logic is applied to isArrayOdd function too.
Then, we can call the two functions sequentially in the main program. If isArrayEven function return true, it display all even message (Line 28-29). If odd, display all odd (Line 30 -31). If neither, display not even or odd (Line 32-33).
Answer:
1. Buffer could be overflowed when input entered by user in command line is greater than size 12 it may of size 13 or larger.
for example: when user enters the string 'dolatsinghsodha' than the size of that string stored in bar variable will be 15 as it not size bounded but when it stored in MyArray[12] than it will overflow the buffer for array because MyArray[] is input bounded to maximum 12 characters.
2. There are two ways to prevent buffer overflow
i) make bar varaible input bounded to 12 characters or
ii) assign MyArray[] size at dynamic time
or change code to following state:
void simple (char *bar) {
int a=bar.length
char MyArray[a];
strcpy(MyArray, bar); // copies bar in MyArray --> Myarray==bar }
}
but it in not possible in some languages and platforms
or we can ask user
Answer:
Following are the code to this question:
file= open('book.txt') #open file
li= {} #define an empty list
for lines in file: # use for loop Split file data into words
d= lines.lower().strip(' !?').split() #define variable d that Add it to map
for val in d: #define loop to store data
if val not in li: #check value is not in list
li[val] = 0 #define list and assign value in 0
li[val] = li[val] + 1 #Sort the book data and add its value
m = sorted(li.items(),key = lambda x : -x[1]) #sorted value into the m variable
print(m[:10]) #print value
Output:
please find the attachment.
Explanation:
In the given python code first, we open the file "book.txt", in next line, an empty list is defined, that uses the for loop which can be described as follows:
- In the for loop is used, that reads the file data, and defines a variable "d", that stores the values into the map.
-
In the next line another loop is used, that check file values, if values are the same type so, it adds values and writes it.
- In the last line, m variable is used, that sorts the values and use the slicing to print its value.