[c]Questions and exercises

Question 1
A 2 minute telephone call to Lexington, Virginia costs $1.15. Each additional minute costs $0.50. Write a program that takes the total length of a call in minutes as input and calculates and displays the cost.

The sample code below:

#include <stdio.h>

int main()
{
    int call_in_mins;
    float cost;
    printf("Call in mins: ");
    scanf("%d",&call_in_mins);
    if(call_in_mins > 2)
    {
        cost = 0.5 * (call_in_mins - 2) + 1.15;
    }
    else
    {
        cost = 1.15;
    }
    printf("Cost on your call will be: %.2f",cost);

    return 0;
}

Question 2
Get the sum of 1^2 + 2^2 + 3^2 + … n^2, where n is the user’s input.

The sample code below:

#include <stdio.h>

int main()
{
    int upper_limit,i,sum=0;
    printf("Enter the upper limit: ");
    scanf("%d",&upper_limit);
    for(i=1;i<=upper_limit;i++)
    {
        //sum += (i*i);
        sum += pow(i,2);
    }
    printf("Sum = %d",sum);
    return 0;
}
Advertisements
Posted in General stuffs | Leave a comment

[c]Decimal to binary

#include <stdio.h>

int main()
{
    /*
    Find the binary of decimal. Example: if 4.
    Find the remainder of a number divider by 2.
    4%2 = 0 >> 1st index.
    2%2 = 0 >> 2nd index.
    1%2 = 1 >> 3rd index.
    Then reverse the index enumerate the number backwards.
    bin[i--] until looks like this 100.
    */
    int i, bin[10], num, count=0;
    printf("Enter a number");
    scanf("%d",&num);
    while(num!=0)
    {
        bin[count] = num%2;//store the remainder.
        count++;
        num /= 2; 
    }
    for(i=count-1;i>=0;i--)//enumerate backwards
    {
        printf("%d",bin[i]);
        
    }
    
    return 0;
}
Posted in General stuffs | Leave a comment

[c]Extension of previous triangle pattern with numbers

Get a mirror image of the pattern on previous post.
This exercise is the same as previous, just print the columns..

here’s the code:

#include <stdio.h>

int main()
{
    int num,rows,cols;
    printf("Enter the rows:");
    scanf("%d",&num);
    
    for(rows=1;rows<=num;rows++)
    {
        for(cols=1;cols=1;rows--)
    {
        for(cols=1;cols<=rows;cols++)
        {
               printf("%d",cols);
            
        }
        printf("\n");
    }
    
    return 0;
}

Looks like this when user enter 5:

Enter the rows:5
1
12
123
1234
12345
1234
123
12
1

Posted in General stuffs | Leave a comment

[c]Nested for loop exercise.

This is a good lesson which trains my logic…

See the lessons here: https://codeforwin.org/2015/07/right-triangle-star-pattern-program-in-c.html

See sample codes:

#include <stdio.h>

int main()
{
    int num,rows,cols;
    printf("Enter the rows:");
    scanf("%d",&num);
    
    for(rows=1;rows<=num;rows++)
    {
        for(cols=1;cols<=rows;cols++)
        {
                printf("%d",cols);
        }
        
        printf("\n");
    }
    return 0;
}

Here is how it looks like when user enters 5:

1
12
123
1234
12345

Algorithm:
1. When row is 1, column is 1, is column less than or equals to 1? Yes, hence 1 is displayed.
2. when row is 2, column goes back to 1, is column less than or equals to 2? Yes, only 1 and 2.
3. when row is 3, column goes back to 1, is column less than or equals to 3? Yes, only 1,2 and 3.
4. when row is 4, column goes back to 1, is column less than or equals to 4? Yes, only 1,2,3,4.
5. when row is 5, column goes back to 1, is column less than or equals to 5? yes all the numbers.

Posted in General stuffs | Leave a comment

[c]working with files

Here’s the sample code. Objective is to open a file name – output.txt, if this file does not exist it will be created by the fopen function.

For this example the file will accept 100 characters. fprintf is to copy the contents over to output.txt.

#include <stdio.h>
#include <stdlib.h>
int main()
{
	FILE *fout;
	char line[100]; //string
	char *filename = "output.txt";
	printf("Enter some text.");
	scanf("%[^\n]s",line); //to accept multiple words
	fout = fopen(filename,"w"); //write mode
	if(filename==NULL)
	{
		printf("Error with file.\n");
		exit(1);
	}
	fprintf(fout,"%s",line); //copy the input data into the file.
	fclose(fout); //save and close.
	return 0;
}
Posted in General stuffs | Leave a comment

[c]Using arrays – 2

1. Physical size array, assign the actual size of the array.
2. Logical size array, the actual size you use.

Here’s the sample code:

#include <stdio.h>

int main()
{
    char text[100]; //physical size of array.
    int i=0;
    
    printf("Enter any text:");
    scanf("%s",text); //For string format specifier no need to use &, accept one word only.
    
    /*Uncomment below to accept multiple words until user press enter.*/
    //scanf("%[^\n]s",text); 
    
    while(text[i]!='\0') //\0 is the terminating character.
    {
        i++;
    }
    printf("Logical size is %d\n",i);
    return 0;
}
Posted in General stuffs | Leave a comment

[c]Using arrays.

The requirement is to take in 10 integers from user, and sort them according to even, odd and negative numbers.

Challenges
1. The numbers are arbitrary, you never know which number types user will put in.
2. the number of even, odd and negative numbers are dynamic, however you do know that maximum number of integers is ten.
3. You will need to print out even, odd and negative numbers of different length because there will be a mixture of odd, even and negative numbers, negative numbers might not be entered and so on…

Here’s the sample code in c:

#include <stdio.h>

int main()
{
    int num,evenList[10],oddList[10],negativeList[10], ei=0,oi=0,ni=0,i;
    printf("Enter 10 integers:\n");
    for(i=0;i<10;i++)
    {
        scanf("%d",&num);
        
        if(num<0)
        {
            negativeList[ni]=num;
            ni++;
        }
        else if(num%2==0)
        {
            evenList[ei]=num;
            ei++;
        }
        else
        {
            oddList[oi]=num;
            oi++;
        }
    }
    printf("Elements in even array\n");
    for(i=0;i<ei;i++)
    {
        printf("%d\t",evenList[i]);
    }
    printf("\n");
    printf("Elements in odd array\n");
    for(i=0;i<oi;i++)
    {
        printf("%d\t",oddList[i]);
    }
    printf("\n");
    printf("Elements in negative array\n");
    for(i=0;i<ni;i++)
    {
        printf("%d\t",negativeList[i]);
    }
    printf("\n");
    return 0;
}

Below is the test sample:

Enter 10 integers:
-10 -10 -10
20
30
40
50
60
87
89
Elements in even array
20 30 40 50 60
Elements in odd array
87 89
Elements in negative array
-10 -10 -10

Posted in General stuffs | Leave a comment