Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Some tasks to practice arrays in C
05-21-2010, 03:19 AM (This post was last modified: 06-18-2010 02:32 AM by drdebcol.)
Post: #1
Some tasks to practice arrays in C
I have a few solved tasks for arrays in programming language C.
Some of them are a bit complicated for beginners, but they are pretty understandable !

So let's start with one-dimensional arrays.
1) Output array in reversed order. (You can not use reverse() function).
This is very simple as you can see :
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  int i,n,a[100];
  printf("Enter length of array : \n");
  scanf("%d",&n);
  printf("Enter array : \n");
  for (i=0;i<n;i++)  
    scanf("%d",&a[i]);
  printf("Reversed : \n");
  for (i=n-1;i>=0;i--)
    printf("%d ",a[i]);
  printf("\n");
  system("pause");
}

2) Input an array of natural numbers (including zero) and output those members of array which sum of digits is bigger than entered number k.
As you can see you need to know "mod div" routine or in C "% /" routine.
Here is the code :
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  unsigned i,n,a[100],k,j,s,t;
  printf("Enter length : \n");
  scanf("%d",&n);
  printf("Enter k : \n");
  scanf("%d",&k);
  printf("Enter members of array : \n");
  for (i=0;i<n;i++)  
    scanf("%d",&a[i]);
  printf("Numbers are : \n");
  for (i=0;i<n;i++)
   {
     s=0;
     t=a[i];
     do
     {
       s+=(t % 10);
       t=t/10;
     }
     while(t>0);
     if (s>k)
       printf("%d ",a[i]);
   }
  printf("\n");
  system("pause");
}

3) Input an array of natural numbers. Output all members of that array if sum of all members before it is bigger than sum of it's digits.
(You don't need to count first member of array).
Here is the code :
Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
  unsigned i,j,a[100],n,scl,sci,k;
  printf("Enter length : \n");
  scanf("%d",&n);
  printf("Enter members : \n");
  for (i=0;i<n;i++)  
    scanf("%d",&a[i]);
  for (i=1;i<n;i++)
    {
      sci=0;
      k=a[i];
      do
      {
        sci+=(k % 10);
        k=k/10;    
      }
      while (k>0);
      scl=0;
      for (j=0;j<i;j++)
        scl+=a[i];
     if (scl>sci)
       printf("%d ",a[i]);  
    }
    printf("\n");
    system("pause");
}

And now Matrix of two-dimensional arrays.

4) Input a square matrix of integers. Output members of matrix that are smaller than difference of sum and product of all members of matrix.
(Calculate sum and product on input)
I know this seems hard, but it is not really.
Here is the code :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
  int i,j,n,a[50][50],k,s=0,p=1;
  printf("Enter size of matrix n*n : \n");
  scanf("%d",&n);
  printf("Enter metrix : \n");
  for (i=0;i<n;i++)
    for (j=0;j<n;j++)
    {
      scanf("%d",&k);
      a[i][j]=k;
      s+=k;
      p*=k;
    }
  printf("Solution is : \n");
  for (i=0;i<n;i++)
    for (j=0;j<n;j++)
      if (a[i][j]<fabs(p-k))
        printf("%d ",a[i][j]);
    printf("%d");
    system("pause");
}

5) Input a square matrix and check if it is symmetrical or not.
(Matrix is symmetrical if for every member satisfy that a(i,j)=a(j,i))
So here is the code :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
  int i,j,n,a[50][50],b=1;
  printf("Enter dimensions of matrix : \n");
  scanf("%d",&n);
  printf("Enter matrix : \n");
  for (i=0;i<n;i++)
    for (j=0;j<n;j++)
      scanf("%d",&a[i][j]);
  for (i=0;i<n;i++)
    for (j=0;j<n;j++)
      if (b)
        if (a[i][j]!=a[j][i])
          b=0;
    if (b)  
      printf("Matrix is symmetrical\n");
    else
      printf("Matrix is not symmetrical \n");
    system("pause");
}
You can do this task using command "break".

That's all. You can post your solutions in other programming languages !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
06-18-2010, 02:36 AM (This post was last modified: 06-18-2010 02:36 AM by drdebcol.)
Post: #2
RE: Some tasks to practice arrays in C
I want to add one more task to this list of tasks.
You need to enter matix m*n and find how many sub-matrices dimensions 2*2 have the specified sum s. You can see that m and n need to be bigger or equal to 2.
Example :
m=4, n=4, s=5
1 2 1 4
0 2 3 1
1 0 2 3
1 4 2 0
Number of sub-matrices is : 1
It is sub-matrix (dimensions 2*2) :
1 2
0 2

Here is the code :
Code:
#include <stdlib.h>
#include <stdio.h>

int main()
{
    int m,n,i,j,a[50][50],s,br=0;
    do
     {
      printf("Enter m and n bigger than one :\n");
      scanf("%d %d",&m,&n);
     }
    while (m<2 || n<2);
    printf("Enter sum :\n");
    scanf("%d",&s);
    printf("Enter matrix : \n");
    for (i=0;i<m;i++)
      for (j=0;j<n;j++)
        scanf("%d",&a[i][j]);
    for (i=0;i<m-1;i++)
      for (j=0;j<n-1;j++)
        if ((a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1])==s)
          br++;      
    printf("Number of sub-matrices is : %d \n",br);  
    system("pause");      
}

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
07-28-2010, 06:08 AM
Post: #3
RE: Some tasks to practice arrays in C
Woah. i just re-read this thread. Very useful info, I'm sorry i skipped over this!

"Character is determined more by the lack of certain experiences than by those one has had."
Friedrich Nietzsche
Visit this user's website Find all posts by this user
Quote this message in a reply
07-31-2010, 04:13 PM
Post: #4
RE: Some tasks to practice arrays in C
Nice examples c arrays can be cumbersome for some people sometimes.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: