Search
Log in
Latest topics
Statistics
We have 30 registered usersThe newest registered user is deigavr
Our users have posted a total of 191 messages in 126 subjects
Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest None
Most users ever online was 125 on Wed Apr 07, 2021 9:18 am
9th program
Page 1 of 1
9th program
Objective
Study the way to use Arrays.
Theory
“A capability that enables the user to define a set of ordered data items known as an array.”
An array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage. Arrays can be classified as fixed-sized arrays (sometimes known as static arrays) whose size cannot change once their storage has been allocated.
Arrays are useful critters because they can be used in many ways. Arrays are essentially a way to store many values under the same name. You can make an array out of any data-type.
Declaration of an array
Like any other variable arrays must be declared before they are used. The general form of declaration is:
datatype variable-name[10];
Multidimensional arrays
The most useful aspect of arrays is multidimensional arrays. Multidimensional arrays can be described as "arrays of arrays". Multi-dimensional arrays are accessed using more than one index.
This is a graphic of what a two-dimensional array looks like when I visualize it.
For example:
datatype variable-name[10][10];
Task
Write a program that sort an integer array of 5.
coding
#include<stdio.h>
void main(void)
{
int i,j,k,temp;
int arr[5];
{
printf("enter 5 numbers=\n");
scanf("%d",&arr[0]);
scanf("%d",&arr[1]);
scanf("%d",&arr[2]);
scanf("%d",&arr[3]);
scanf("%d",&arr[4]);
for(i=0;i<5;i++)
{
for(j=0;j<4-i;i++)
{
if (arr[j]>arr[j+1])
{
arr[j]=temp;
arr[j]=arr[j+1];
arr[j+1]=temp;
}
for (k=0;k<=4;k++)
{
printf("%d",&arr[k]);
}
2. Write a program that accepts values into two 2x2 arrays. Calculate the sum of the two matrices and store the matrix result in another array. Also display the result on the screen.
#include <stdio.h>
void main (void)
{
int t[2][2],y[2][2],u[2][2];
printf ("ENTER 1st MATRIX");
printf ("\nEnter 1ST ROW\n");
scanf ("%d",&t[0][0]);
scanf ("%d",&t[0][1]);
printf ("\nENTER 2ND ROW\n");
scanf ("%d",&t[1][0]);
scanf ("%d",&t[1][1]);
printf ("\nMATRIX 2\n");
printf ("ENTER FIRST ROW\n");
scanf ("%d",&y[0][0]);
scanf ("%d",&y[0][1]);
printf ("\nENTER 2ND ROW\n");
scanf ("%d",&y[1][0]);
scanf ("%d",&y[1][1]);
u[0][0]=t[0][0]+y[0][0];
u[0][1]=t[0][1]+y[0][1];
u[1][0]=t[1][0]+y[1][0];
u[1][1]=t[1][1]+y[1][1];
printf ("\nSum\n%d %d\n%d %d",u[0][0],u[0][1],u[1][0],u[1][1]);
}
Study the way to use Arrays.
Theory
“A capability that enables the user to define a set of ordered data items known as an array.”
An array is a data structure consisting of a group of elements that are accessed by indexing. In most programming languages each element has the same data type and the array occupies a contiguous area of storage. Arrays can be classified as fixed-sized arrays (sometimes known as static arrays) whose size cannot change once their storage has been allocated.
Arrays are useful critters because they can be used in many ways. Arrays are essentially a way to store many values under the same name. You can make an array out of any data-type.
Declaration of an array
Like any other variable arrays must be declared before they are used. The general form of declaration is:
datatype variable-name[10];
Multidimensional arrays
The most useful aspect of arrays is multidimensional arrays. Multidimensional arrays can be described as "arrays of arrays". Multi-dimensional arrays are accessed using more than one index.
This is a graphic of what a two-dimensional array looks like when I visualize it.
For example:
datatype variable-name[10][10];
Task
Write a program that sort an integer array of 5.
coding
#include<stdio.h>
void main(void)
{
int i,j,k,temp;
int arr[5];
{
printf("enter 5 numbers=\n");
scanf("%d",&arr[0]);
scanf("%d",&arr[1]);
scanf("%d",&arr[2]);
scanf("%d",&arr[3]);
scanf("%d",&arr[4]);
for(i=0;i<5;i++)
{
for(j=0;j<4-i;i++)
{
if (arr[j]>arr[j+1])
{
arr[j]=temp;
arr[j]=arr[j+1];
arr[j+1]=temp;
}
for (k=0;k<=4;k++)
{
printf("%d",&arr[k]);
}
2. Write a program that accepts values into two 2x2 arrays. Calculate the sum of the two matrices and store the matrix result in another array. Also display the result on the screen.
#include <stdio.h>
void main (void)
{
int t[2][2],y[2][2],u[2][2];
printf ("ENTER 1st MATRIX");
printf ("\nEnter 1ST ROW\n");
scanf ("%d",&t[0][0]);
scanf ("%d",&t[0][1]);
printf ("\nENTER 2ND ROW\n");
scanf ("%d",&t[1][0]);
scanf ("%d",&t[1][1]);
printf ("\nMATRIX 2\n");
printf ("ENTER FIRST ROW\n");
scanf ("%d",&y[0][0]);
scanf ("%d",&y[0][1]);
printf ("\nENTER 2ND ROW\n");
scanf ("%d",&y[1][0]);
scanf ("%d",&y[1][1]);
u[0][0]=t[0][0]+y[0][0];
u[0][1]=t[0][1]+y[0][1];
u[1][0]=t[1][0]+y[1][0];
u[1][1]=t[1][1]+y[1][1];
printf ("\nSum\n%d %d\n%d %d",u[0][0],u[0][1],u[1][0],u[1][1]);
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Mon Aug 01, 2011 11:44 pm by Guest
» Netbook Brands
Mon Aug 01, 2011 5:39 am by Guest
» backlinks checker backlink service
Sun Jul 31, 2011 8:56 am by Guest
» how to buy facebook fans f4
Sat Jul 30, 2011 2:34 pm by Guest
» Alle bijzondere dingen in de zaanstreek
Sat Jul 30, 2011 8:11 am by Guest
» HERE YOU CAN POST ALL WEBSITE LINKS...
Wed Aug 26, 2009 2:16 pm by onesimpletech
» cool web site for all must check it
Mon Jul 06, 2009 2:32 pm by Snopy Cobra
» poetry spirit
Mon Jul 06, 2009 2:31 pm by Snopy Cobra
» best to learn VC++
Sun May 10, 2009 3:22 pm by Guest