5.1.
Write a program that produces 20 random numbers between 1 and 20.
The program should store all non-duplicate values in an array (s.t., first value in Array[0], second value in Array[1], …)
Use a single array to accomplish this task.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int check_array(int array[],int i){
int j;
for(j=0;j<i;j++){
if(array[i]==array[j])
return 1;
}
return 0;
}
int main()
{
int array[20];
int i,j;
srand(time(NULL));
printf("Nonrepetitive array values are:\n");
for(i=0;i<20;i++){
array[i]=rand()%20+1;
if(check_array(array,i))
break;
printf("Array[ %d ] = %d\n",i,array[i]);
}
return 0;
}
5.2.
Write a recursive “binarySearch” function to perform the binary search of the array.
The function should receive an integer array, the starting subscript, the ending subscript and the search key as arguments.
If the search key is found, return the array subscript; otherwise, return -1.
#include<stdio.h>
#define SIZE 15
int binarySearch(const int b[], int searchKey, int low, int high);
void printHeader(void);
void printRow(const int b[], int low, int mid, int high);
int main()
{
int a[SIZE];
int i;
int key;
int result;
for(i=0;i<SIZE;i++)
a[i]=2*i;
printf("Enter a number between 0 and 28: ");
scanf("%d",&key);
printHeader();
result=binarySearch(a,key,0,SIZE-1);
if(result!=-1)
printf("\n%d found in array element %d\n",key,result);
else
printf("\n%d not found\n",key);
return 0;
}
void printHeader(void){
int i;
printf("\nSubscripts: \n");
for(i=0;i<SIZE;i++)
printf("%3d ",i);
printf("\n");
for(i=1;i<=4*SIZE;i++)
printf("-");
printf("\n");
}
void printRow(const int b[], int low, int mid, int high){
int i;
for(i=0;i<SIZE;i++){
if(i<low||i>high)
printf(" ");
else if(i==mid)
printf("%3d*",b[i]);
else
printf("%3d ",b[i]);
}
printf("\n");
}
int binarySearch(const int b[], int searchKey, int low, int high){
int middle;
middle=(low+high)/2;
printRow(b,low,middle,high);
if(low>high)
return -1;
if(b[middle]==searchKey)
return middle;
else if(b[middle]<searchKey)
return binarySearch(b,searchKey,middle+1,high);
else
return binarySearch(b,searchKey,low,middle-1);
}
5.3.
Write program that adds two matrices by adding the corresponding entries together.
The function should receive three integer matrices, two input matrices and one result matrix, as arguments.
Using rand() function to initialize two input matrices
row: 5, column: 6, range of random numbers: 1 ~ 100
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void summat(int mat1[][6],int mat2[][6],int mat3[][6]);
int main()
{
int i, j;
int mat1[5][6],mat2[5][6],mat3[5][6];
srand(time(NULL));
printf("\nmatrix1:\n");
for(i=0;i<5;i++){
for(j=0;j<6;j++){
printf("%4d",mat1[i][j]=rand()%100+1);
}
printf("\n");
}
printf("\nmatrix2:\n");
for(i=0;i<5;i++){
for(j=0;j<6;j++){
printf("%4d",mat2[i][j]=rand()%100+1);
}
printf("\n");
}
summat(mat1,mat2,mat3);
printf("\nmatrix3:\n");
for(i=0;i<5;i++){
for(j=0;j<6;j++){
printf("%4d",mat3[i][j]);
}
printf("\n");
}
return 0;
}
void summat(int mat1[][6],int mat2[][6],int mat3[][6]){
int i,j;
for(i=0;i<5;i++){
for(j=0;j<6;j++)
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
}
5.4.
Write a recursive function stringReverse that takes a character array as an argument, prints it back to front and returns nothing.
The function should stop processing and return when the terminating null character of the string is encountered.
char strArray[30] = “Print this string backward.”;
void stringReverse( char strArray[] );
#include<stdio.h>
void stringReverse(char strArray[]);
int main()
{
char strArray[30]="Print this string backward.";
int i;
for(i=0;i<30;i++)
printf("%c",strArray[i]);
printf("\n");
stringReverse(strArray);
return 0;
}
void stringReverse(char strArray[]){
if(strArray[0]=='\0')
return;
stringReverse(&strArray[1]);
printf("%c",strArray[0]);
}
5.5.
Write program that sum the 2x2 matrix element and print the largest 2x2 matrix and the smallest 2x2 matrix.
Using rand() function to initialize matrix (1~9 integer number)
The function should receive one integer matrix, and print the largest value, the smallest value and start point (I, j) each value
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void findmat(int mat[][5]);
int main()
{
int mat[5][5];
int i,j;
srand(time(NULL));
printf("Matrix\n");
for(i=0;i<5;i++){
for(j=0;j<5;j++)
printf("%4d",mat[i][j]=rand()%9+1);
printf("\n");
}
findmat(mat);
return 0;
}
void findmat(int mat[][5]){
int i, j, sum,val_largest=0, val_smallest=36, largest[2],smallest[2];
for(i=0;i<4;i++){
for(j=0;j<4;j++){
sum=mat[i][j]+mat[i][j+1]+mat[i+1][j]+mat[i+1][j+1];
if(sum>val_largest){
val_largest=sum;
largest[0]=i;
largest[1]=j;
}
if(sum<val_smallest){
val_smallest=sum;
smallest[0]=i;
smallest[1]=j;
}
}
}
printf("The largest square value : %d (%d,%d)\n",val_largest,largest[0],largest[1]);
printf("The smallest square value : %d (%d,%d)\n",val_smallest,smallest[0],smallest[1]);
}
'학부공부 > C_Basic' 카테고리의 다른 글
7. String[C Basic] (0) | 2021.11.07 |
---|---|
6. Pointer[C Basic] (0) | 2021.11.07 |
4. Function[C Basic] (0) | 2021.11.07 |
3. Loop[C Basic] (0) | 2021.11.07 |
2. Loop[C Basic] (0) | 2021.11.06 |
댓글