# Use the following data to find the velocity and acceleration [Solved]

Question 1

Question 2

Write a C Program: Write a program that sorts a series of words entered by the user:

Enter word: foo

Enter word: bar

Enter word: baz

Enter word: quux

Enter word: In sorted order: bar baz foo quux

Assume that each word is no more than 20 characters long. Stop reading when the user enters an empty word (i.e., presses enter without entering a word). Store each word in a dynamically allocated string using an array of pointers to keep track of the string, as in the remind2.c program in section 17.2 of your book. After all words have been read, sort the array (using any sorting technique) and then use a loop to print the words in sorted order. HINT: Use the read line function to read each word as in remind2.c

The normal strcmp() library function is case sensitive which means “eye” and “EYE” would be considered as unequal. However using the strcasecmp() library function call, the string comparison will ignore the differences in case. It means “eye” and EYE” would be considered as equal.

Question 3

A small firm makes three similar products, which all follow the same three-step process, consisting of milling, inspection, and drilling. Product A requires 12 minutes of milling, 5 minutes for inspection, and 10 minutes of drilling per unit; Product B requires 10 minutes of milling, 4 minutes for inspection, and 8 minutes of drilling per unit; and product C requires 8 minutes of milling, 4 minutes for inspection and 16 minutes of drilling. The department has 20 hours available during the next period of milling, 15 hours for inspection and 24 hours for drilling. Product A contributes \$2.40 per unit to profit, product B \$2.50 per unit and product C \$3 per unit.

1. Formulate the problem as a linear programming problem. Clearly state the decision variables, objective function and the constraints.

2. Can this problem be solved using graphical method? Explain why.

3. Solve the problem using Solver. Determine the optimum solution (how much of each product needs to be made), and the profit.

Decision variables are used for optimizing solution to aid decision-making. The optimization of linear programming is formulated within the particular constraints applied to the decision variable.

Question 4

Write a complete program to do the following:

(a) Write a function called readdata() which receives two parameters, an integer n and an array of integers called vals, both of which are changed in the function. The function reads a value into n and reads in n integers, storing the data values in the array vals. Print the data values as they are read in.

(b) Write a function called countzeros() which receives two parameters, an integer n and an array vals. The function counts how many of the first n elements of the vals array are 0. Print the number of 0 values (in either the main program or the function). Forexample,ifthearrayholds 66 0 -4 0 4 31withn=6,ithastwo0values.

(c) Write a function called append() which reads in several new values into the array, putting them at the end. As a result, it must change both the array and the value of n. The function receives the same two parameters as readdata(). Assume the array initially holds 66 0 -4 0 4 31 with n = 6; after the function call, the array might hold 66 0 -4 0 4 31 22 0 49 with n = 9.

(d) Write a main program which calls these functions. First, the main program calls readdata() to read a set of data into an array called numbers, which contains no more than 100 integers. The number of elements actually read is returned by the function and stored in a variable called size. Then the main program calls the function countzeros() to find how many of the size array elements are 0. Next the main program calls append() to modify the numbers array and n. The append() function reads in new values until input failure, adding the new values to the array and incrementing n. The new values in the array are printed (in either the main program or the function). Finally, the main program calls the function countzeros() again to determine how many elements in the new array are 0.

In C++ programming language, one can concatenate two strings using the + operator as shown below.

string fname = “John “;

string lname = “Rambo”;

string fullname = fname + lname; // fullname = John Rambo

The algorithm for sorting the given set of strings is as follow:

Step 1 : Start

Step 2 : define array of char pointers

Step 3 : get the string from the user till user enters empty string

Step 3.1: allocate memory for the string

Step 3.2: copy the string to the allocated memory, maintain count of the number of string provided by the user.

Step 4 : call SortString()and pass the array of pointers and the count of the number of strings entered by the user.

Step 4.1: The SortString()uses bubble sort algoritm. And to take care of the capital and small letters in the given strings, we use strcasecmp()library function.

Step 5 : display the sorted strings

Step 6 : dealloacate the memory allocated in Step 3

Step 7 : stop

1. #include <stdio.h>
2. #include <string.h>
3. #include<stdlib.h>
4. #define MAX_CHAR_IN_GIVEN_WORD 20 /* this can be changed as required */
5. #define MAX_NO_OF_ENTRY 20 /* this can be changed as required */
6. void SortString(char *arr[],int sizeOfarray); /* function declaration */
7. int main()
8. {
9. char *data[MAX_NO_OF_ENTRY]; /* to hold the input strings from the user */
10. char wordEntered[MAX_CHAR_IN_GIVEN_WORD] = {‘a’,’a’,’a’,’a’,’a’,’a’,’\0′};
11. int counter = 0;
12. int arrayIndex = 0;
13. while( wordEntered != ‘\0’ )
14. {
15.     printf(“\n Enter word : “);
16.     gets(wordEntered);
17.     /* store each word in a dynamically allocated string */
18.     data[counter] = (char *)malloc(sizeof(char) * 20);
19.
20.     strcpy(data[counter],wordEntered);
21.
22.     counter++;
23.
24. }
25.
26.     SortString(data,counter-1);
27.
28. /* the string array is now sorted */
29.
30.     printf(“\n \n Sorted output : “);
31.     for (arrayIndex = 0; arrayIndex < counter;arrayIndex++)
32.      printf(” %s “,data[arrayIndex]);
33.
34.
35.     // deallocate the memory */
36.     for (arrayIndex = 0; arrayIndex < counter;arrayIndex++)
37.     free(data[arrayIndex]);
38. return(0);
39. }/* end of program */
40. /* Function definition */
41. void SortString(char *intArray[],int sizeOfarray)
42. { /* this function uses the bubble sort algorithm to sort the given strings */
43. int firstIndex,secondIndex;
44. char temp[MAX_CHAR_IN_GIVEN_WORD]; /* use for swapping */
45.        for (firstIndex = 0; firstIndex < sizeOfarray-1; firstIndex++)
46.        {
47.
48.
49.     for (secondIndex = 0; secondIndex < sizeOfarray-1; secondIndex++)
50.             {
51.
52.                if (strcasecmp(intArray[secondIndex],intArray[secondIndex+1]) > 0)
53.                {/* Swapping the contents */
54.
55.                            strcpy(temp,intArray[secondIndex]);
56.            strcpy(intArray[secondIndex],intArray[secondIndex+1]);
57.            strcpy(intArray[secondIndex+1],temp);
58.
59.                }
60.             } /* end of inner for loop */
61.        }/* end of outer for loop */
62.
63.
64.
65. }/* end of function */

1. Decision Variables:

A = No. of units of Product A to be produced

B = No. of units of Product B to be produced

C = No. of units of Product C to be produced

Objective functions—To maximize the profits

Z = 1.8A + 1.5B + 3C

Constraints:

12A + 10B + 8C <= 1200 (milling time in minutes)

5A + 4B + 4C <= 900 (inspection in minutes)

10A+8B +16C <= 1440 (drilling in minutes)

A, B, C >=0 (non-negativity constraints)

1. #include <iostream>
2. using namespace std;
3. // The function reads a value into n and reads in n integers, storing the data values in the
4. // array vals. Print the data values as they are read in.
5. void readdata(int &n, int vals[])
6. {
7.      cout << ” Enter the number of integers to be read : “;
8.  cin >> n;
9.      for (int i = 0; i < n ; i ++)
10.  {
11.      cout << ” Enter the integer input number #” << i+1 <<” : “;
12.          cin >> vals[i];
13.  }
14. }
15. int countzeros(int n, int vals[])
16. {
17.      int count = 0;
18.  for (int i = 0; i < n ; i ++)
19.  {
20.      if (vals[i] == 0)
21.     count++;
22.  }
23.  return count;
24. }
25. // this function will return the number of integers appended
26. void append(int &n, int vals[])
27. {
28. char userChoice = ‘y’;
29. cout << ” \n “;
30. while ( (userChoice == ‘y’) || (userChoice == ‘Y’) )
31. {
32.    cout << ” Enter the integer input number to be appended to the existing array : “;
33.    cin >> vals[n];
34.    n++;
35.    cout << ” Do you want to continue ( y / n ) ? “;
36.    cin >> userChoice;
37. }
38. }
39. string mappingInOnesDigit(int val)
40. {
41. string wordStr;
42. switch (val)
43. {
44.   case 1:
45.        wordStr = “One”;
46.    break;
47.   case 2:
48.        wordStr = “Two”;
49.    break;
50.   case 3:
51.        wordStr = “Three”;
52.    break;
53.   case 4:
54.        wordStr = “Four”;
55.    break;
56.   case 5:
57.        wordStr = “Five”;
58.    break;
59.   case 6:
60.        wordStr = “Six”;
61.    break;
62.   case 7:
63.        wordStr = “Seven”;
64.    break;
65.   case 8:
66.        wordStr = “Eight”;
67.    break;
68.   case 9:
69.        wordStr = “Nine”;
70.    break;
71.   default:
72.       wordStr = ” “;
73.       break;
74.     }
75. return wordStr;
76. }
77. string mappingInTensDigit(int val)
78. {
79. switch (val)
80. {
81. case 2:
82.        return “Twenty”;
83.    break;
84.   case 3:
85.        return “Thirty”;
86.    break;
87.   case 4:
88.        return “Forty”;
89.    break;
90.   case 5:
91.        return “Fifty”;
92.    break;
93.   case 6:
94.        return “Sixty”;
95.    break;
96.   case 7:
97.        return “Seventy”;
98.    break;
99.   case 8:
100.        return “Eighty”;
101.    break;
102.   case 9:
103.        return “Ninety”;
104.    break;
105.   case 10:
106.        return “Hundred”;
107.    break;
108.   default:
109.       return ” UNKO “;
110.    break;
111.     }
112. }
113. string mapNumberToWord(int i)
114. {
115. string wordStr;
116. if ( (i >= 1 ) && (i <= 9) )
117. {
118.   wordStr = mappingInOnesDigit(i);
119.   return wordStr;
120.     }
121.
122. switch (i)
123. {
124.   case 0:
125.        wordStr = “Zero”;
126.    break;
127.   case 10:
128.        wordStr = “Ten”;
129.    break;
130.   case 11:
131.        wordStr = “Eleven”;
132.    break;
133.   case 12:
134.        wordStr = “Twelve”;
135.    break;
136.   case 13:
137.        wordStr = “Thirteen”;
138.    break;
139.   case 14:
140.        wordStr = “Fourteen”;
141.    break;
142.   case 15:
143.        wordStr = “Fifteen”;
144.    break;
145.   case 16:
146.        wordStr = “Sixteen”;
147.    break;
148.   case 17:
149.        wordStr = “Seventeen”;
150.    break;
151.   case 18:
152.        wordStr = “Eighteen”;
153.    break;
154.   case 19:
155.        wordStr = “Nineteen”;
156.    break;
157.   case 20:
158.        wordStr = “Twenty”;
159.    break;
160.   case 30:
161.        wordStr = “Thirty”;
162.    break;
163.   case 40:
164.        wordStr = “Forty”;
165.    break;
166.   case 50:
167.        wordStr = “Fifty”;
168.    break;
169.   case 60:
170.        wordStr = “Sixty”;
171.    break;
172.   case 70:
173.        wordStr = “Seventy”;
174.    break;
175.   case 80:
176.        wordStr = “Eighty”;
177.    break;
178.   case 90:
179.         wordStr = “Ninety”;
180.    break;
181.   case 100:
182.        wordStr = “Hundred”;
183.    break;
184.   default:
185.
186.       int quot = i / 10;
187.    int rem = i % 10;
188.       wordStr = mappingInTensDigit(quot);
189.       wordStr = wordStr + mappingInOnesDigit(rem);
190.       break;
191.     }
192. return wordStr;
193. }
194. /* main function */
195. int main()
196. {
197.      int size;
198.      const int MAX_SIZE_OF_ARRAY = 100;
199.      int values[MAX_SIZE_OF_ARRAY];
200.      int numOfZeroes;
201.      string numberInWord;
202.
204.      numOfZeroes = countzeros(size,values);
205.
206.
207.      numberInWord = mapNumberToWord(numOfZeroes);
208.      cout << “\n The array has ” << numberInWord << ” 0 values. ” << endl;
209.
210.      append(size,values);
211.      numOfZeroes = countzeros(size,values);
212.      numberInWord = mapNumberToWord(numOfZeroes);
213.      cout << “\n The array has ” << numberInWord << ” 0 values. ” << endl;
214.
215.      return 0;
216. }
Categories Q&A