ITU INTRODUCTION TO SCIENTIFIC AND ENGINEERING COMPUTATION Midterm 1

İTÜ BIL104E Midterm 1 Questions
C programlama dersi
Prof. Dr. Murat Yeşiloğlu
 Declare an 9-dimentional array whose elements are the numbers of your student ID

1. Write a function that takes two random numbers from this array.

2. Write a function that shows your Student-ID by asterix sign.
For example: 040120523

 ****

*
**

*****
**
***

3. Write a function that descends the elements of this array and return the array in the main function.

4. Write a function that construct a rectangle whose first edge’s size is sum of the first 3 elements of the array and size of the second edge is sum of the last 3 elements of the array. Return the area of the biggest circle that fits into this rectangle.

5. Write a function where random numbers are taken until the same numbers as your student ID. For each digit, add the trial numbers until your student ID is found from random numbers.

6. Write a function that finds most used numbers in your student-ID.



Solution for Questions
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int frequent(int s[]);
int rand2(int a[]);
int trial(int a[]);
int asterix(int s[]);
int most(int s[]);
int area(int s[]);

int main(){
srand(time(NULL));
int s[10]={5,0,4,1,0,2,1,0,7};
printf("Most frequent number: %d\n",frequent(s));
printf("Random 2 numbers    : %d\n",rand2(s));
printf("Toplam deneme sayısı: %d\n",trial(s));
asterix(s);
}

int frequent(int s[]){
int i,x,j,max,maxindex;  
int frequentf[9]={0};
for(i=0;i<9;i++)
{
j=s[i];    
frequentf[j]++;
} // frequentf  3 2 0 1 1 0 1 0 0
max=frequentf[0];
for(i=1;i<9;i++){
if(frequentf[i]>max){
max=s[i];
}
}
i=0;
while(max!=frequentf[i]){
if(frequentf[i]==max){
break;
}

i++;
}

return i;
}

int rand2(int a[]){
//I dont know how to return more than one value at once
int index[2],i;
for(int i=0;i<2;i++){
index[i]=a[rand()%10];
}
for(i=0;i<2;i++){
return index[i];
}

}

int trial(int a[]){  // oldu

int trial=0;
int j=0;
int i=0;

for(int i=0;i<9;i++){



while(a[i]!=rand()%10){
trial++;

}
printf("%d kadar %d\n",a[i],trial);
}
return trial;
}


int asterix(int a[]){
int i,j;
for(i=0;i<9;i++){


for(j=0;j<a[i];j++){

printf("*");
}
printf("\n");
}
}

HW-1
Questions
Problem 1:
Write a C program that creates following shape and writes this shape into “shape.txt” file. (Using for loops and matrix array)




Problem 2:
Write a C program that reads 5 words with maximum six letters and displays them in dictionary order.
{
e.g. “exam”,”enter”,”escape”,”eleven”,”engine”
“eleven”, “engine”, “enter”, “escape”, “exam”
}

Comments

Popular Posts