Tuesday, January 12, 2016

(ENG/TR) Algorithm to find digits of number (Sayının numaralarını bulmak için algoritma)

ENGLISH

How to find digits of N-digit number. It means we don't know how many digits does number have.

Ex.
N = 6
Number = 153756

ANSWER

First way:

Actually, first of all, we have to think that how we find number of digits of number in real life?
-When we read the number, actually we simplify this number. For example: 1324. How we read it? 
-One thousand three hundred twenty four.
-Actually we split number into digits. Then, second question: How we split it in programming world?
-Dividing by 10! Yes, every time we divide this number, we lose last digit. And when we finally lost last digit(which is 1 for 1324) we will have 0.
-So if we copy this number to another variable(so that we won't lose it while dividing), then count how many times we divide this number to get zero; we can find the number of digits.
-Now we have number of digits N! What we do is get the modulus 10 of number (to find last digit because when we divide number with 10 and look remainder, we see last digit of number) and save it to array, then divide ten, N times.

Second way(more efficient):

You can find digits of number while dividing 10. Up to get zero, take last digit and divide number with 10. It is more simple and complicated way.

TÜRKÇE

Birinci yol:

Aslında burada sizden istenen, girilen bir sayının kaç basamaklı olduğunu bulmanızdır. Örnek verelim: 10562. Bu sayının 5 basamaklı olduğunu anlamak için aslına bakarsanız basamaklarına ayırıyoruz. "on bin beş yüz altmış iki" diye okuyunca 5 basamaklı olduğunu anlıyoruz. O zaman bu sayıyı programlama dilinde de basamaklarına ayırmalıyız. Aslında bu işlemin nasıl yapıldığını biliyorsunuz: Sürekli 10'a bölerek. Ta ki 0 kalana kadar. O zaman yapmamız gereken bir "counter" la bu işlemi kaç kere yaptığımızı saymak.
Tabii ki bölmeden önce sayımızı farklı bir variable'a atayalım ki o sayımızı kaybetmeyelim.
Daha sonra sayımızın son rakamını bulup, bunu bir array'e atayıp ardından tekrar 10 a bölüp tekrar son rakamı bulabiliriz. Son rakamı bulmak için 10'a bölüp kalana bakarız. Bu da mod 10 demektir. Yani ilk önce mod 10'unu alıp sonra 10'a böleriz. Bu işlemi de basamak sayısı kadar tekrarlarız.

İkinci yol(daha etkili):

Daha etkili bir yol olarak 0 elde edene kadar 10'a bölüp her böldüğünüzde son basamağı bulabilirsiniz. Bu daha etkili ve daha basit bir yöntem olur.

(C Language) (ENG/TR) Correct Mistakes In The Code 1 (Koddaki hataları düzeltiniz 1)

void main()
{
  int x,y;
  char *secim;
  float sonuc;
  printf("iki Sayi Giriniz=");
  scanf("%d %d",&x,&y);
  printf("Seciminiz=");
  scanf("%s",&secim);
  switch (secim)
  {
    case '+':sonuc=x+y;break;
    case '-':sonuc=x-y;break;
    case '/':sonuc=x/y;break;
    case '*':sonuc=x*y;break;
    default :
      printf("Yanlis islem");break;
  }
printf("Sonuc = %f\n",sonuc);
getch();
}

Resource/Kaynak: http://www.frmtr.com/c-ve-c-/6767426-c-ve-c-da-char-sorunu.html#post61080352

Question Owner/Soru Sahibi: hemdehtml

ENGLISH

When you try to create string of characters (like char *secim), this string -if you don't initialize with values- it's last element
-which is our only element- will be null.

"char *secim;" actually means "char *secim = {'\0'}". When you try to assign new value in this pointer, you are trying to resize it which can not be possiblw. Because we should always have null value at the end of string.

You can improve your code by initializing x and y as floats. Because if our result is float, then we lost data because of int initialization. Because at the end if our result is 10/7 = 1,..., the result will be 1 because inputs are integer.

The full code is below.

TÜRKÇE

Bir karakter dizisi yaratmaya çalıştığınızda (char *secim gibi), bu dizi -initialize durumunda bir değer atamazsanız şayet- son elemanını (yani bizim var olan tek yerimizi) "null" karakteriyle dolduruyor. Somutlaştırırsak:

"char *secim;" demek aslında "char *secim = {'\0'};" demekle aynı şeydir. Ve siz yaptığınız işlemde bu "null" karakterinin yerine başka bir karakter atamaya çalışıyorsunuz. Yani "secim[1]" i "secim[2]" yapmaya çalışıyorsunuz. Çünkü bildiğimiz üzere her dizinin son elemanı "null" yani '\0' olmak zorundadır.

Sorununuzu "char secim[2];" yazarak basitçe çözebilirsiniz.

siz bir pointer tanımladınız. pointerlar yanına yıldız koymazsanız adresi tanımlarlar. O yüzden switch içerisindeki "secim" in solunda yıldız olmalı. ve sadece "secim" yazarsak bir adresi belirteceği için, scanf fonksiyonunun içinde "secim" in yanına "address-of" operatörünü koymamalıyız.

kodunuzu iyileştirmek için integer olarak tanımladığınız x ve y 'yi float olarak tanımlayabilirsiniz. Çünkü int karakterlerle işlem yaparken küsürlü bir sonuç elde ederseniz, bu sonucun sadece tam kısmı tutulur. Dolayısıyla bu kısmı float'a aktarırken veri kaybı yaşarsınız. Örnek olarak 10/7=1,... olması gerekirken, int olarak tanımlamanızdan dolayı sonucu 1 olarak çıktıda görürsünüz.

Kodun tamamını aşağıda bulabilirsiniz.

CODE/KOD


void main()
{
  float x, y;
  char secim[2];
  float sonuc;
  printf("iki Sayi Giriniz=");
  scanf("%d %d", &x, &y);
  printf("Seciminiz=");
  scanf("%s", secim);
  switch (*secim)
  {
    case '+':sonuc = x + y; break;
    case '-':sonuc = x - y; break;
    case '/':sonuc = x / y; break;
    case '*':sonuc = x*y; break;
    default:
      printf("Yanlis islem"); break;
  }
printf("Sonuc = %f\n", sonuc);
getchar();
}

(ENG/TR) Algorithm to Sort Given Names (Verilen isimleri sıralamak için algoritma)

ENGLISH

You have to sort given names with selection sort algorithm.

Resource: http://www.frmtr.com/c-ve-c-/6765984-dizilerde-siralama-algoritmalari.html#post61077366

Question Owner: desera53



TÜRKÇE

Verilen isimleri "seçerek sıralama" algoritmasını kullanarak sıralayınız.

Kaynak: http://www.frmtr.com/c-ve-c-/6765984-dizilerde-siralama-algoritmalari.html#post61077366

Soru Sahibi: desera53

(C Language) (ENG/TR) Increment-Detriment (Kar-Zarar)

ENGLISH


You win X TL from lottery and deposit to bank. This person can get 150% of X as loan from bank.
This person get some money(Y) from bank before get loan. Then get loan, after that, get all of his/her money from bank.
This person should pay loan to the bank with 15% interest. Draw flow diagram of this operations which also computes person's increment or detriment.
And write C program of these operations.

Resource: http://www.frmtr.com/c-ve-c-/6761814-zarar-hesaplayan-algoritma.html#post61077115
Question Owner: babayara123

ANSWER



TÜRKÇE

"X TL piyango çıkan bir kişi bu parayı bir hesaba yatırmıştır. Hesap
açılışından sonra hesabında X TL bulunan bu kişinin çekebileceği kredi ise
hesabındaki paranın %150’sidir. Kredi çekmeden önce bir miktar para (Y) çeken
hesap sahibi kendi hak ettiği kredinin tamamını ve kendi hesabındaki parayı
çekecektir. Bir müddet sonra hesap sahibi çektiği krediyi %15 faizi ile birlikte
bankaya ödeyecektir. Bu hesap sahibinin varsa zararını hesaplayan akış
diyagramını çiziniz ve C programını yazınız. ( X ve Y klavyeden girilecektir X>Y)"

Kaynak: http://www.frmtr.com/c-ve-c-/6761814-zarar-hesaplayan-algoritma.html#post61077115
Soru Sahibi: babayara123




Sunday, March 15, 2015

(C Language) (ENG) Sum Square Difference - Project Euler Problem 6

PROBLEM

In this problem, you asked to find difference between "square of summation of first 100 natural numbers" and "summation fo squares of first 100 natural numbers".

For example from Project Euler:

The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025

Difference: 3025 − 385 = 2640.



This c problem is taken by me from: (https://projecteuler.net/problem=6)


SAMPLE RUN:

In addition to problem, i printed result on the screen.




SOLUTION

You can copy codes below to your compiler to execute.




#include <stdio.h>

int main(void) //main function.
{

int sumOfSq = 0; //it will hold summation of squares.
int sqOfSum = 0; //it will hold square of summation.

for (int k = 1; k <= 100; k++) //this loop will finde summation of squares.
sumOfSq = sumOfSq + k * k;
for (int k = 1; k <= 100; k++) //this loop will find summation of first 100 natural numbers.
sqOfSum = sqOfSum + k;
sqOfSum = sqOfSum * sqOfSum; //this line will find square of summation.

printf("difference between summation of squares and square of summation is %d", sqOfSum - sumOfSq); //this line will find difference and print it on the screen.

return 0;
}

(C Language) (ENG) Smallest Multiple - Project Euler Problem 5

PROBLEM

For example 2520 is the smallest positive number which can be divided by integers from 1 to 10(included) with remainder '0'.

What is the smallest positive number which can be divided by integers from 1 to 20(included) then?

This c problem is taken by me from: (https://projecteuler.net/problem=5)

SAMPLE RUN:

In addition to problem, i printed result on the screen.




SOLUTION

You can copy codes below to your compiler to execute.




#include <stdio.h>

int main(void) //main function.
{

int spn = 20; //smallest positive number that mentioned.
int flag = 0; //we will use it to get out from loop or continue.
int counter = 0; //this counter will show program how many times spn was divided with remainder 0.

while(flag == 0)
{
spn++; //this code will update spn to check next value.
for (int k = 1; k <= 20; k++) //this loop will find if spn is divisible for all integers 20 >= x > 1 with 0 remainder.
{
if (spn % k == 0)
counter++;
else break;
}
if (counter == 20) //if it is true, it means that we find spn that I searched.
flag = 1;
counter = 0; //this code will turn counter to 0 to use it again.
}

printf("smallest positive number is %d", spn);

return 0;
}

Friday, March 13, 2015

(C Language) (ENG) Even Fibonacci Numbers - Project Euler Problem 2

PROBLEM



Write a program calculating summation of even Fibonacci numbers starting from "1, 2, 3, 5, 8, 13..." below 4.000.000.

This c problem is taken by me from: (https://projecteuler.net/problem=2)

SAMPLE RUN:

In addition to problem, i printed result on the screen.





SOLUTION

You can copy codes below to your compiler to execute.


#include <stdio.h>

int main(void) //main function.
{
int sum = 0; //this variable will hold summation of even Fibonacci numbers.
int a, b, c; //due to I can find summation with only 3 elements, I will use 3 elements.

a = 1; //in this question, our series starts with 1...
b = 2; //...and continuous with 2.
c = 0; //this line causes c to get into loop.

while (c < 4000000) //this loop will find the summation of even numbers.
{
c = a + b; //it will find the next element's value.

if (c % 2 == 0) //this comparison will determine if c is even or not.
sum = sum + c; //it will calculate summation of c.
a = b; //we can keep next value(which is b after a) in a with that line.
b = c; //we can keep next value(which is c after b) in a with that line.
}

sum = sum + 2; // in loop above, we find the summation except b that we initialized which is also even.

printf("Summation of even Fibonacci numbers below 4.000.000 is %d", sum); //it will print the summation on the screen.

return 0;
}