Professional Documents
Culture Documents
ID: 1951075
------------------------------------------------------------------------------------------------------------
Pay attention when you hand in the solution of programming topics:
- The solution of each topic is packed in a Zip file including the file .cpp, .exe,
…
- then, all solutions of a part are packed altogether in a Zip file named
part1.zip, part2.zip, …
The solutions do NOT follow the rule are not accepted.
----------------------------------------------------------------------------------------------------------
Part 1.
1. Input a vector (1 D array) of n integers. Check and print on screen all prime numbers.
2. Input 2 D array of size n. Print out the sum of all prime numbers in the array.
3. Input a vector (1 D array) of n values. Sort them out in ascending order. Print out the
list sorted.
4. Input a string of characters. Check the first letter of each word, and change it to
capital letter if available.
Part 2.
1. Input a matrix of size n if int (2D array of n x n). Print out the sum of all even and
positve numbers. Use functions for doing those.
2. Input a matrix of size n if int (2D array of n x n). Print out the sum of all prime
numbers. Use functions for doing those.
3. Input a dynamic matrix of size n if int (2D array of n x n). Print out the sum of all
even and positve numbers. Use functions for doing those.
4. Input n strings of characters. Sort these strings in ascending order, then them out on
screen.
Part 3.
English text book: 16.1 – 16.13
1. Input a vector (1 D array) of n integers. Check and print on screen all prime
numbers.
#include <stdio.h>
#define MAX 100
void PrimeCheck(int x,int *NotPrime);
main(){
int i,n,number[MAX],NotPrime;
do{
printf("Enter the size of vector:");
scanf("%d",&n);
if (n<=0) printf("The value is invalid. Please enter again");
} while (n <= 0);
printf("Enter numbers: ");
for (i=0;i<n;i++)
scanf("%d",&number[i]);
printf("Prime number are: ");
for (i=0;i<n;i++){
PrimeCheck(number[i],&NotPrime);
if (NotPrime == 0)
printf("%d ",number[i]);
};
}
2. Input 2 D array of size n. Print out the sum of all prime numbers in the array.
#include <stdio.h>
#define MAX 100
main()
{
int i,j,n,number[MAX][MAX],NotPrime,sum=0;
do{
printf("Enter the size of matrix:");
scanf("%d",&n);
if (n<=0) printf("The value is invalid. Please enter again");
}while (n <= 0);
input(number,n);
for (i=0;i<n;i++)
for(j=0;j<n;j++){
PrimeCheck(number[i][j],&NotPrime);
if (NotPrime == 0)
sum+= number[i][j];
}
printf("Sum of prime numbers = %d",sum);
}
main(){
int i,j,n,number[MAX][MAX],ans=0;
do{
printf("Enter the size of matrix:");
scanf("%d",&n);
if (n<=0) printf("The value is invalid. Please enter again");
}while (n <= 0);
input(number,n);
sum(number,n,&ans);
printf("Sum of prime numbers = %d",ans);
}
if (first = '\0')
return -1;
while (word[i] != '\0')
word[i - 1] = word [i];
word[i] = first;
word[i + 1] = 'a';
word[i + 2] = 'y';
word[i + 3] = '\0';
}
16.2 Write a C program that accepts a list of numbers from the user until a number
is repeated (i.e., is the same as the number preceding it). The program then prints
out the number of numbers entered (excluding the last) and their sum. When the
program is run, the prompts and responses will look like the following:
#include <stdio.h>
main()
{
int number[100];
int i=0,n,sum=0;
do{
printf("Number ");
scanf("%d",&number[i]);
n = i;
i++;
} while (number[n]!=number[n-1]);
for (i=0;i<n;i++)
sum += number[i];
printf("%d numbers were entered and their sum is ",n,sum);
}
16.3 What is the output when the following code is compiled and run?
intx;
i nt main{ )
{
i nt *p x = &x;
i nt x = 7;
* p x = 4;
printf(" x = %d\n" , x) ;
}
x=7
16.4) Create a string function that takes two input strings, string A and stringB ,
and returns a 0 if both strings are the same, a 1 if string A appears before string B
in the sorted order of a dictionary, or a 2 if string B appears before stringA.
#include <stdio.h>
#include <string.h>
int str_compared(char strA[],char strB[]);
main(){
int a;
char strA[100];
char strB[100];
a = str_compared(strA,strB);
printf("%d",a);
}
int str_compared(char strA[],char strB[]){
printf("String A: ");
gets(strA);
printf("String B: ");
gets(strB);
if (strcmp(strA,strB) == 0)
return 0;
else if (strcmp(strA,strB) < 0)
return 1;
else return 2;
}
16.5 Using the function developed for Exercise 16.4, modify the Insertion Sort
program so that it operates upon strings instead of integers.
void insertionSort(char* list[])
{
int unsorted;
int sorted;
char *unsortedItem;
16.7 For this question, examine the following program. Notice that the variable ind
is a pointer variable that points to another pointer variable.
Such a construction is legal in C.
#includ e <stdio.h >
i n t main( )
{
i nt apple ;
i nt *ptr ;
i nt **ind ;
i n d = &ptr ;
*in d = dapple ;
**in d - 123 ;
ind++ ;
*ptr++ ;
apple++ ;
print f (, ! %x %x %d\n H , ind, ptr , apple) ;
}
Analyze what this program performs by drawing out the run-time stack
at the point just after the statement apple++ ; executes.
A snapshot of the run-time stack is shown in the table below. Memory values are shown
in the right-most column.
0xEFF8 Ind 0xEFFA
0xEFF9 Ptr 0xEFFA
0xEFFA Apple 125
0xEFFB saved frame pointer …
0xEFFC saved return address …
0xEFFD return value …
16.9 Write a program to remove any duplicates from a sequence of numbers. For
example, if the list consisted of the numbers 5, 4, 5, 5, and 3, the program would
output 5, 4, 3.
/* The list contains MAXNUMS integers */
/* Also, all duplicate elements are converted to 0 */
void RemoveDuplicates(int list[])
{
int i;
int j;
int unique_list = 0;
int found;
for (i = 0; i < MAXNUMS; i++) {
found = 0;
for (j = 0; j < unique_list; j++) {
if (list[j] == list[i])
found = 1;
}
if (!found) {
list[unique_list] = list[i];
unique_list++;
}
}
/* clean up the remainder of the list */
for (j = unique_list; j < MAXNUMS; j++)
list[j] = 0;
return;
}
16.13 Consider the following declarations.
#defin e STACK_SIZ E 100
int stack[STACK_SIZE] ;
int topOfStack ;
int Push(in t item) ;
a. Write a funtion Push (the declaration is provided) that will push the value of item
onto the top of the stack. If the stack is full and the item cannot be added, the
function should return a 1. If the item is successfully pushed, the function should
return a 0.
b. Write a function Pop that will pop an item from the top of the stack. Like Push,
this function will return a 1 if the operation is unsuccessful. That is, a Pop was
attempted on an empty stack. It should return a 0 if successful. Consider carefully
how the popped value can be returned to the caller.