C Programming Language Tutorial (Advanced) – Lect 1 Structures

This is the very first lecture on Structures ie struct in C programming language. We will study the basics of struct. Why do we need it and how did it come into picture. In this lecture we will be covering struct creation, initialization, accessing elements of structure and also how to dynamically take input from the user and store it inside structure.

Code 1


#include<stdio.h>
int main(){
//Declaration
    struct student{
        char name[20];
        int roll;
        int age;
    };
   
    //Create struct variable
    struct student s1={"Bob",3,15};
   
    //Accessing struct variable
    printf("Name: %s \n",s1.name);
    printf("Roll Number: %d \n",s1.roll);
    printf("Age: %d \n",s1.age);
   
return 0;
}

code 2


#include<stdio.h>
int main(){
   
    //Declaration
    struct student{
        char name[20];
        int roll;
        int age;
    };
   
    //Create struct variable
    struct student s1;
   
    printf("Enter your name ");
    scanf("%s",s1.name);
    printf("Enter your roll number ");
    scanf("%d",&s1.roll);
    printf("Enter your age ");
    scanf("%d",&s1.age);
   
   
    //Accessing struct variable
    printf("Name: %s \n",s1.name);     
    printf("Roll Number: %d \n",s1.roll);
    printf("Age: %d \n",s1.age);

    return 0;
}


Previous Lecture : Arrays and Pointers

Playlist : c-programming-tutorials

Facebook Page

Linking Within Same Page and Different Pages

HTML Tutorials for Beginners – Linking Within Same Page and Different Pages
This tutorial teaches how to link different pages in our website and also how we can give link to some content on the same page using “id” attribute. In this video series we will study basics of html. We will study basics tags in HTML with their meaning.

NOTE: Pages that are to be linked in our website should be placed in the same folder as that of index.html

The following codes are used in the video.

Source code for index.html


<!DOCTYPE html>
<html>
<body>
<!-- link to some other page of our website -->
<p>
<a href="page-two.html">Check out page two</a>
</p>

<!-- link to content on same page -->
<p>
<a href="#heading-three">Check out heading three</a>
</p>
<p>
<a href="#heading-seven">Check out heading seven</a>
</p>

<h3>This is heading one</h3>
<p>Some content that describes the heading</p>

<h3>This is heading two</h3>
<p>Some content that describes the heading</p>

<h3 id="heading-three">This is heading three</h3>
<p>Some content that describes the heading</p>

<h3>This is heading four</h3>
<p>Some content that describes the heading</p>

<h3>This is heading five</h3>
<p>Some content that describes the heading</p>

<h3>This is heading six</h3>
<p>Some content that describes the heading</p>

<h3 id="heading-seven">This is heading seven</h3>
<p>Some content that describes the heading</p>

<h3>This is heading eight</h3>
<p>Some content that describes the heading</p>

<h3>This is heading nine</h3>
<p>Some content that describes the heading</p>

<h3>This is heading ten</h3>
<p>Some content that describes the heading</p>


</body>
</html>

Source code for page-two.html


<!DOCTYPE html>
<html>
<body>

<p>
<a href="index.html">Check out my home page</a>
</p>

<h2>This is page two</h2>


</body>
</html>

Source code for page-three.html


<!DOCTYPE html>
<html>
<body>

<p>
<a href="page-two.html">Check my page two</a>
</p>

<h2>You are on page three</h2>


</body>
</html>

Source code for page-four.html


<!DOCTYPE html>
<html>
<body>

<p>
<a href="index.html">Check out my home page</a>
</p>

<h2>You are on page four</h2>


</body>
</html>

Previous : HTML Introduction Anchor and Image tag
Category : HTML TUTORIAL

Break String using index and separator

wethecomputerguys-BreakString

- Read the string character by character and store it in output string until a separator is encountered.
– Create a count variable that keeps a track of number of times this separator has occurred.
– If count equals index value print the output string.
– Else increment the count and make output string blank.



#include<stdio.h>
#include<string.h>


int main(){
	char str[1000];
	char ans[100]="";
	int i,count=0,index,pos=0;
	char separator;
	
	printf("Enter String\n");
	gets(str);
	
	printf("Enter index\n");
	scanf("%d",&index);
	
	fflush(stdin);
		
	printf("Enter Separator\n");
	scanf("%c",&separator);
	
	
	// read string char by char
	for(i=0;i<strlen(str);i++){

                // check if char is separator
		if(str[i]==separator){
                        
                         // if separator count equals index
			if(count==index)
				break;
			count++;
			pos=0;
		}else{
			ans[pos++]=str[i];
		}
	}
	
	if(count!=index)
		printf("Not Possible\n");
	else{
		ans[pos]='';
		printf("Output string is %s",ans);
	}
	
		
	return 0;
}

Transpose of a matrix

wethecomputerguys-transpose of a matrix

Transpose Of a Matrix is simply interchanging
the rows and columns of the matrix.

Note: If you have a n rows and m columns matrix the
transpose matrix will have m rows and n columns.(nXm -> mXn)

So for this program we will use two matrices
matrix —-> stores original matrix
transposeMatrix —-> stores transpose of original matrix


#include<stdio.h>
int main(){
    int n,m;
   
    int matrix[20][20];
    int transposeMatrix[20][20];
   
    printf("Enter number of rows in original matrix\n");
    scanf("%d",&n);
    printf("Enter number of columns in original matrix\n");
    scanf("%d",&m);
   
    printf("Enter elements of matrix\n");
    int i,j;
   
    //Take input for Matrix
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){   
            scanf("%d",&matrix[i][j]);
        }
    }
   
    //Transpose the matrix by interchanging rows and columns
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            transposeMatrix[j][i]=matrix[i][j];
        }
    }
   
    printf("The Original Matrix is \n");
    for(i=0;i<n;i++){
       
        for(j=0;j<m;j++){
           
            printf("%d\t",matrix[i][j]);
        }
       
        printf("\n");
    }
   
    printf("The Transposed Matrix is \n");
    for(i=0;i<m;i++){
       
        for(j=0;j<n;j++){
           
            printf("%d\t",transposeMatrix[i][j]);
        }
       
        printf("\n");
    }
   
    return 0;

}

HTML Introduction Anchor and Image tag

HTML Tutorials for Beginners – HTML Introduction Anchor and Image tag
In this video series we will study basics of html. We will study basics tags in HTML with their meaning.
In this series, HTML Tutorials for Beginners we will start from scratch and learn how the web pages on the internet are made. In this series we will be assuming the that you have never programmed in your life, so anyone can follow along. As the series progress we will also look into HTML5 the latest standard in the internet technology and discuss some of the features of it.

This video explains linking of webpages using anchor tag and also to display an image in a web page or a web document.

Explains about few important attributes of anchor a tag like target and image img tag like size and alternate text (alt) and also explains how image can be used to with an anchor tag.

<!DOCTYPE html>
<html>

<body>

<!-- target="_blank" opens the link in a new tab or a window -->
<h2>Linking a webpage using an anchor tag</h2>
<a href="http://www.wethecomputerguys.com" target="_blank">Visit our blog</a>
<!-- inbetween anchor tag anything can be placed - text, sentence, image or any element -->

<h2>Image without height and/or width attribute</h2>
<img src="batman_logop.png" alt="Batman Logo">

<h2>Image with height and/or width attribute</h2>
<img src="batman_logop.png" alt="Batman Logo" height="400px">

<h2>Image with height and/or width attribute using style</h2>
<img src="batman_logop.png" alt="Batman Logo" style="height:400px">

<!-- change value of src attribute with the name and extenstion of image file on your computer -->

<h2>Anchor tag around an image</h2>
<a href="http://en.wikipedia.org/wiki/Batman">
<img src="batman_logop.png" alt="Batman Logo" style="height:100px; width:150px;">
</a>

</body>

</html>

Previous : HTML Introduction Tutorial
Next : Linking Within Same Page and Different Pages
Category : HTML TUTORIAL

SUM OF UNIQUE ELEMENTS IN AN ARRAY

https://wethecomputerguys.files.wordpress.com/2014/11/wethecomputerguys-sumofuniqueelements

Approach 1:

1.Input each element of the array.
2.Initialize a variable sum to 0.
3.For each element in the array check whether that number is repeated.
– If no add the number to the sum.
4. Output sum.

Time complexity of this algorithm is N^2.


#include<stdio.h>
int main(){
	int arr[1000],i,j,sum=0,n;
	printf("Enter number of elements\n");
	scanf("%d",&n);
	
	printf("Enter elements of the array\n");
	for(i=0;i<n;i++){
		scanf("%d",&arr[i]);
	}
	
	
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(arr[i]==arr[j] && i!=j){
				break;
			}
		}
		if(j==n)
			sum+=arr[i];
		}
	
	printf("Sum of unique elements is %d.",sum);
	
	return 0;
}


The same could be solved in time less than N^2

WetheComputerGuys-SumOfUniqueElements

Approach 2:

1. Decide the upper limit of your input number. In the code MAX taken in 1000. Now all numbers should be less than 1000.
2. Create a variable sum and initialize to 0.
3. Create an array of size 1000. Say array(arr) and initialize it to 0
4. For each element increment arr[element]
For example suppose input is 4 then increment arr[4].
Fig. shows this for numbers less than 10.
5. Iterate over the array arr and if value is 1 add it’s index value to sum.
In the fig. the circled elements have value 1 and so it’s index which is highlighted would be added.
6. Output sum

In figure it’s assumed that numbers are less than 10.


Time complexity of this algorithm is N.


#include<stdio.h>
#define MAX 1000
int main(){
	int arr[MAX]={},i,j,sum=0,n,input;
	printf("Enter number of elements\n");
	scanf("%d",&n);
	
	printf("Enter elements of the array\n");
	for(i=0;i<n;i++){
		scanf("%d",&input);
		arr[input]++;
	}
	
	for(i=0;i<MAX;i++){
		if(arr[i]==1)
			sum+=i;
	}
			
	printf("Sum of unique elements is %d.",sum);
	
	return 0;
}

Pattern Printing – 40

wethecomputerguys-Printing Pattern

To print triangular pattern we need two loops.
Inner Loop and Outer Loop
Inner Loop prints the rows
Outer Loop prints columns
In this pattern the logic observed is the decreasing difference
in adjacent columns. Go through the image for visualization.
The difference between 1st and second column is numberOfRows-1
ie one less than number of rows.
And the next difference decreases by one.


#include<stdio.h>
int main(){
   
    int n;
    printf("Enter number of rows\n");
    scanf("%d",&n);
   
    int i,j;
    for(i=1;i<=n;i++){
       
        //Initialize difference
        int difference=n-1;
       
        //Initialize print variable
        int print=i;
       
        for(j=1;j<=i;j++){
           
            //-3 used for formatting purpose
            printf("%-3d",print);
           
            //Add difference for next column
            print+=difference;
           
            //Decrease the difference by 1
            difference--;
           
        }
       
        printf("\n");
       
    }
    return 0;

}

Previous: Pattern Printing 39
Category: Pattern Printing

HTML Introduction Tutorial

HTML Tutorials for Beginners-HTML Introduction Tutorial
This is the very first lecture on HTML basics. We will study basics tags in HTML with their meaning.
In this series, HTML Tutorials for Beginners we will start from scratch and learn how the web pages on the internet are made. In this series we will be assuming the that you have never programmed in your life, so anyone can follow along. As the series progress we will also look into HTML5 the latest standard in the internet technology and discuss some of the features of it.

<!DOCTYPE html>
<html>
<body>

<h1>Hello World</h1>

<h2>Hello World</h2>

<h3>Hello World</h3>

<h4>Hello World</h4>

<h5>Hello World</h5>

<h6>Hello World</h6>

<p>I can see some <b>bold text</b> in this paragraph</p>

<p>I can see some <strong>strong text</strong> in this paragraph</p>

<p>I can see some <i>italic text</i> in this paragraph</p>

<p>I can see some <em>emphasized text</em> in this paragraph</p>

<p>I can see some <sub>subscript</sub> text</p>

<p>I can see some <sup>superscript</sup> text</p>


</body>
</html>

Next : HTML Introduction Anchor and Image tag
Category : HTML TUTORIAL

Factorial of large Numbers

WeTheComputerGuys- Factorial

The value of factorial is very big integer and so cannot be stored in an integer data type. So we store the numbers as strings and multiply 2 strings . Multiply two strings the way we multiple 2 numbers manually on a paper.

WeTheComputerGuys- Factorial


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define MAX 10000

// Function to multiple 2 big numbers in the same way
// manual multiplication is done

char * multiply(char a[],char b[]){
    static char mul[MAX];
    char c[MAX];
    char temp[MAX];
    int la,lb;
    int i,j,k=0,x=0,y;
    long int r=0;
    long sum = 0;
    la=strlen(a)-1;
        lb=strlen(b)-1;
   
        for(i=0;i<=la;i++){
                a[i] = a[i] - 48;
        }

        for(i=0;i<=lb;i++){
                b[i] = b[i] - 48;
        }

    for(i=lb;i>=0;i--){
         r=0;
         for(j=la;j>=0;j--){
             temp[k++] = (b[i]*a[j] + r)%10;
             r = (b[i]*a[j]+r)/10;
         }
         temp[k++] = r;
         x++;
         for(y = 0;y<x;y++){
             temp[k++] = 0;
         }
    }
   
    k=0;
    r=0;
    for(i=0;i<la+lb+2;i++){
         sum =0;
         y=0;
         for(j=1;j<=lb+1;j++){
             if(i <= la+j){
                 sum = sum + temp[y+i];
             }
             y += j + la + 1;
         }
         c[k++] = (sum+r) %10;
         r = (sum+r)/10;
    }
    c[k] = r;
    j=0;
    for(i=k-1;i>=0;i--){
         mul[j++]=c[i] + 48;
    }
    mul[j]='\0';
    return mul;
}


// Function to calculate factorial
char * factorial(int x){
	static char ans[MAX];
	ans[0] = '1';
	ans[1] = '\0';
	int i,j;
	char* tem;
	
	for(i=1;i<=x;i++){
		char temp[MAX];
		itoa(i,temp,10);
		tem = multiply(ans,temp);
		for(j=0;j<strlen(tem);j++){
			ans[j]=tem[j];
		}
	}
	return ans;
}


int main(){
    int a;
    char *c;
    int la,lb;
    int i=0;
    printf("Enter the number : ");
    scanf("%d",&a);
    printf("Factorial of number is : ");
    c=factorial(a);
    while(c[i]=='0')
    	c++;
    printf("%s",c);
    return 0;
}



Pattern Printing – 39

doubt 19-1

-Firstly we take input of height and width.
– We divide the pattern in 3 parts.
– Topmost row
– Central region
– Bottom row

- For topmost row:

doubt 19-2
– Print numbers from 1 to width (In the below code we have used count variable and initialized it to 0 and increment till width)
– Newline

- For central region:

doubt 19-3
– For each row in this region
– Print the value of count and increment it.
– Now Print spaces (Number of spaces equal to width – 2). Also for each space printed increment the value of count.
– Print the value of count and increment it.
– Newline

- For last row:

doubt 19-4
-Print numbers starting from count and increment it. (Total numbers printed in this row equals width)
– Newline

NOTE: In the below code %3d is used in printf for formatted output.


#include<stdio.h>
int main(){
    int heigth, width,i,j,count=1;
    printf("Enter Height\n");
    scanf("%d",&heigth);

    printf("Enter Width\n");
    scanf("%d",&width);

    // Topmost row
    for(i=0;i<width;i++)
        printf("%3d",count++);

    printf("\n");

    // Central region
    for(i=1;i<heigth-1;i++){
        printf("%3d",count++);
        for(j=1;j<width-1;j++){
            printf("   ");
            count++;
        }
        printf("%3d\n",count++);
    }

    // Bottommost row
    for(i=0;i<width;i++){
        printf("%3d",count++);
    }


    return 0;
}

Previous: Pattern Printing 38
Next : Pattern Printing 40
Category: Pattern Printing

Follow

Get every new post delivered to your Inbox.

Join 45 other followers