C: Vectors - Applications resolved


1) Declaring a string (one-dimensional table or arrays) with initialization values.
#include<stdio.h>
void main()
{
	int a[]={2,91,0,-45};	/*an array of integer elements, 
				initialized array variable was declared. 
				Allocated memory for this array comes 
				directly from the number of elements 
				that is initilized */
 
	// display string containing integers
	for(int i=0; i<4; i++)
		printf("%d", a[i]);
 
	getchar();
	int k;
	scanf("%d", k);
}
2) Storing information in a one-dimensional table by reading.
#include<stdio.h>
void main()
{
	int a[20];	/*declared a string of maximum 20 elements of type int named "a" */
 
	// Enter data into string
 
	// enter the number of elements in the string
	int n;
	printf("Enter the number of elements in the string (<=20): ");
	scanf("%d", &n);
 
	//introducing elements from string
	printf("Enter the elements: \n");
	for(int i=0; i<n; i++)
	{
		printf("\t elem %d: ", i+1);
		scanf("%d", &a[i]);
	}
 
	//Show elements from string
	printf("String elements are: ");
	for(int i=0; i<n; i++)
		printf("%d", a[i]);
 	
	getchar();
	int k;
	scanf("%d", k);
}
3) Write functions to read and display the whole string of integers, after it has previously been declared.
#include<stdio.h>
void read(int a[], int* n)
{
	printf("Enter the number of elements of the string: ");
	scanf("%d",n);
	printf("\nEnter the string elements:\n");
	for(int i=0;i<*n;i++)
	{
		printf("\tEnter elem %d: ",i+1);
		scanf("%d",&a[i]);
	}
}
void display(int a[], int n)
{
	printf("\nString elements are: ");
	for(int i=0;i<n;i++)
		printf("%d ",a[i]);
	
}
void main()
{
	int a[20],n;
	read(a,&n);
	display(a,n);
 
	getchar();
	int k;
	scanf("%d", &k);
}
4) Write functions to read and display the whole string of integers, after it has previously been declared. It requires building a proper type for retaining strings of integers.
#include<stdio.h>
typedef int vector[20]; /*own type vector created for retention 
			strings with a maximum size of 20 elements*/
void read(vector a, int* n)
{
	printf("Enter the number of elements of the string: ");
	scanf("%d",n);
	printf("Enter the string elements:\n");
	for(int i=0;i<*n;i++)
	{
		printf("\telem %d: ",i+1);
		scanf("%d",&a[i]);
	}
}
void display(vector a, int n)
{
	printf("String elements are: ");
	for(int i=0;i<n;i++)
		printf("%d ",a[i]);
}
void main()
{
	vector a;
	int n;
	read(a,&n);
	display(a,n);
 
	getchar();
	int k;
	scanf("%d", &k);
}
5) Dynamic memory allocation of a string. Read and then display a range of integers for which occupied space will be determined at runtime rather than at compile time.
#include<stdio.h>
#include<stdlib.h> 

//memory allocation function for the number of items read in string

int* allocation(int* a, int* n)
{
	printf("Enter the number of elements of the string: ");
	scanf("%d",n);
	a=(int*)malloc(*n*sizeof(int)); /* allocate dynamic memory (on the heap) 
				to store a string of n elements dynamically*/
	if(!a)
	{
		printf ("Allocation error");
		return 0;
	}
	return a;
} 
//function for the introduction of elements in string
void read(int* a, int n)
{
	printf("Enter the string elements:\n");
	for(int i=0;i<n;i++)
	{
		printf("\telem %d: ",i+1);
		scanf("%d",&a[i]);
	}
} 
//function for displaying string elements
void display(int* a, int n)
{
	printf("String elements are: ");
	for(int i=0;i<n;i++)
		printf("%d ",a[i]);
}
void main()
{
	int* a, n;
	a=allocation(a,&n); //call the memory allocation function
	read(a,n); //call the read function
	display(a,n); //call the function display

	getchar();
	int k;
	scanf("%d", &k);
}
6) Will develop an application for string representation of integers, namely:
- will cause a data type representations for it
- read a string of integers number
- will display a whole string number
- will gather string components (we present two methods: one to transfer results by type result and other transfer by line parameters)
- will be multiply string components
- average of string elements
- number negative components and number positive components of string
- minimum and maximum string elements
- sorting ascending string
- search string return item anotmit which position is I found, if the item is several times in different positions, we first result which position it had found.
Functions will be described with explicit area, meaning, the function prototype.
#include<stdio.h>
#include<stdlib.h>
typedef int* vector;
 
//allocation function on memory allocation in the heap for a declared string 
vector allocation(vector,int*); /*first parameter is the string, and the second dimension
						(number of elements to be read)*/
				
 
//read function a string element
void read(vector,int);
 
//displaying a string of integers
void display(vector,int);
 
//sum of the elements in the sequence - to transfer the result type
int sum1(vector,int);
 
//sum of the elements in the sequence - to transfer the line parameters
void sum2(vector,int,int*); //3rd parameter will hold the result of the sum
 
//product string elements
int product(vector,int);
 
//average of string elements
float average(vector,int);
 
//number of negative elements, namely positive, in the string
void no_neg_poz(vector,int,int*,int*);
 
//minimum and maximum values ??from string
void min_max(vector,int,int*,int*);
 
//sorted function to string
void sorting(vector,int);
 
/* looking for a specific item, the return position that is found
in a string (if the item is found several times in
string, returns the first position at which it was) */
int search(vector,int,int);
 
void main()
{
	vector v;
	int n; 
	v=allocation(v,&n); //function call allocation  

	read(v,n); //function call read

	display(v,n); 
	int s1=sum1(v,n);
	printf("\n\nsum of the elements: %d",s1); 
	int s2;
	sum2(v,n,&s2);
	printf("\nsum of the elements: %d",s2);
	int p=product(v,n);
	printf("\nproduct of the elements: %d",p);
	float m=average(v,n);
	printf("\naverage of the elements: %.2f",m); 
	int neg,poz;
	no_neg_poz(v,n,&neg,&poz);
	printf("\nno. of negative elem.: %d; no. of positive elem.: %d",neg,poz);
 	int min,max;
	min_max(v,n,&min,&max);
	printf("\nMinimum of string: %d; Maximum of string: %d",min,max);
 	sorting(v,n);
	display(v,n);
 	int elem;
	printf("\nEnter an element that wants to be looked: ");
	scanf("%d",&elem);
	if(search(v,n,elem)) //result different from 0
		printf("\tElement is on position %d", search(v,n,elem));
	else
	printf("\tElement not in string");
 
	getchar();
	int k;
	scanf("%d", &k);
}
vector allocation(vector a, int* n)
{
	printf("Enter the number of elements: ");
	scanf("%d",n);
	a=(int*)malloc(*n*sizeof(int));
	return a;
}
void read(vector a, int n)
{
	printf("Enter the elements:\n");
	for(int i=0;i<n;i++)
	{
		printf("\telem %d= ",i+1);
		scanf("%d",&a[i]);
	}
}
void display(vector a, int n)
{
	printf("\nString is: ");
	for(int i=0;i<n;i++)
	printf("%d ",a[i]);
}
int sum1(vector a, int n)
{
	int s=0;
	for(int i=0;i<n;i++)
	s+=a[i]; //s=s+a[i];
	return s;
}
void sum2(vector a, int n, int* s)
{
	*s=0;
	for(int i=0;i<n;i++)
	*s+=a[i];
}
int product(vector a, int n)
{
	int p=1;
	for(int i=0;i<n;i++)
	p*=a[i];
	return p;
}
float average(vector a, int n)
{
	return (float)sum1(a,n)/n;
}
void no_neg_poz(vector a, int n, int* neg, int* poz)
{
	*neg=*poz=0;
	for(int i=0;i<n;i++)
	if(a[i]>=0)
		(*poz)++;
	else
		(*neg)++;
}
void min_max(vector a, int n, int* m, int* M)
{
	*m=*M=a[0]; /*go from the premise that the first element will be both element
					minimum and maximum in string*/
	for(int i=1;i<n;i++)
	if(a[i]<*m)
	*m=a[i];
	else
	if(a[i]>*M)
	*M=a[i];
}
void sorting(vector a, int n)
{
	int aux;
	for(int i=0;i<n-1;i++)
	for(int j=i+1;j<n;j++)
	if(a[i]>a[j]) //sorting ascending
	{
		aux=a[i];
		a[i]=a[j];
		a[j]=aux;
	}
}
int search(vector a, int n, int e)
{
	for(int i=0;i<n;i++)
	if(a[i]==e)
	return i+1;
	return 0;	
}
7) Reading and display a string of numbers by building a proper type for retention string of integers. The function will be described with explicit area, meaning, the function prototype.
#include<stdio.h>
#include<malloc.h>
typedef int* vector;
vector allocation (vector a, int* n)
{
  printf("Enter the number of elements: ");
  scanf("%d",n);
  a=(int*)malloc(*n*sizeof(int));
  return a;
}
void read(vector a, int n)
{
 printf("Enter the elements:\n");
 for(int i=0;i<n;i++)
   {
    printf("\telem %d= ",i+1);
    scanf("%d",&a[i]);
   }
}
void display(vector a, int n)
{
 printf("\nString is: ");
 for(int i=0;i<n;i++)
   printf("%d ",a[i]);
}
void main()
{
  vector x; //the string of integers
  int n;  //number of elements to be read in string
  
  x=allocation(x,&n);
  read(x,n);
  display(x,n);
 
 getchar();
 int k;
 scanf("%d",k);
}
8) We have declarations: int a [20], int n, float s; Determine the value of the expression e in each case: e = x1 + x2 + x3 + ... + xn.
#include<stdio.h>
void read(int a[], int* n)
{
	printf("\nEnter the number of elements: ");
	scanf("%d", n);
	printf("\nEnter the elements: ");
	for(int i=0; i<*n; i++)
	{
		printf("\nElem %d: ", i+1);
		scanf("%d", &a[i]);
	}
}
int expression(int a[20],int n)
{	
	int e=0;
	for(int i=0; i<n; i++)
	{
	e=e+a[i];
	}
	return e;
}
void main()
{
	int a[20], n, e;
 
	read(a, &n);
	printf("\nThe result is: %d", expression(a,n));
 
	getchar();
	int var;
	scanf("%d", &var);
}
9) Dynamic allocation string.
#include <stdio.h>
#include <stdlib.h>
void main()
{
    int n , *a, i;
    printf ("Enter the number of elements of the string: ");
    scanf ("%d", &n); 
    //memory allocation for dynamically storing a string of n elements
    a=(int*) malloc(n*sizeof(int));  
    //introduction of elements in string
    printf ("Enter the elements in string: \n"); 
    for (i=0; i<n; i++)
	{
        printf ("element %d: ", i+1);
		scanf ("%d", &a[i]);
	} 
	//display elements
    printf ("The integers string elements are: \n");
    for (i=0; i<n; i++)
	{
       	printf ("element %d = %d\n", i+1, a[i]);
	}
 
	int j;
	scanf("%d", &j);
}
10) We have declarations: int a [20], int n, float e; Determine the value of the expression e in each case:
a) e = x1 + x2 + x3 + ... + xn;
b) e = x1 * x2 * x3 * ... * xn;
c) e = average of the components of the vector;
d) e = sum of squares of vector components;
e) e = negative components of the vector sum of cubes;
f) e = x1-x2 + x3-x4 + ... ± xn
#include<stdio.h>

//data entry in string
void read(int b[], int* n)
{
	printf("\nEnter the number of elements: ");
	scanf("%d", n);
 
	printf("\nEnter the elements:\n ");
	for(int i=0; i<*n; i++)
	{
		printf("\nElem %d:", i+1);
		scanf("%d", &b[i]);
	}
}
int expression1 (int a[20], int n)
{
	int e=0;
	for(int i=0; i<n; i++)
	e=e+a[i];
	return e;
}
int expression2 (int a[20], int n)
{
	int e=1;
	for(int i=0; i<n; i++)
	e=e*a[i];
	return e;
}
float expression3 (int a[20], int n)
{
	float e=0;
	for(int i=0; i<n; i++)
		e=e+a[i]/(float)n;
	return e;
}
int expression4 (int a[20], int n)
{
	int e=0;
	for(int i=0; i<n; i++)
	e=e+a[i]*a[i];
	return e;
}
int expression5 (int a[20], int n)
{
	int e=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]<0)
		e=e+a[i]*a[i]*a[i];
	}
	return e;
}
int expression6 (int a[20], int n)
{
	int e=0,sign=1;
	for(int i=0;i<n;i++)
	{
		if (i%2==0)
			sign=1;
		else 
			sign=-1;
 		e=e+a[i]*sign;
	}
	return e;
}
void main()
{
	int b[20], n, i;
	float e; 
	read(b, &n); 
	printf("\n\tSum of the elements is: %d",expression1(b,n));
	printf("\n\tThe product is: %d",expression2(b,n));
	printf("\n\tThe arithmetic average is: %.2f",expression3(b,n));
	printf("\n\tSum of squares is: %d",expression4(b,n));
	printf("\n\tThe sum of negative cubes is: %d",expression5(b,n));
	printf("\n\tExpression is: %d",expression6(b,n));
 
	getchar();
	int var;
	scanf("%d", &var);
}
11) Accomplish a program that merge two vectors in a third vector. The first two will be initially sorted.
#include<stdio.h>
#include<stdlib.h>
void main()
{ 
 int n , m, *a, *b, i, j, found, aux, k, *c; 
//---------------------------------------------------------------------------------
    printf ("Enter the number of elements of string 1: ");
    scanf ("%d", &n);
	//memory allocation for dynamically storing a string of n elements
	a=(int*) malloc(n*sizeof(int));
   //introduction of elements in string
	printf ("Enter the elements in string 1: \n"); 
	for (i=0; i<n; i++)
	{
        printf ("element %d: ", i+1);
		scanf ("%d", &a[i]);
	}
//---------------------------------------------------------------------------------

    printf ("Enter the number of elements of string 2: ");
    scanf ("%d", &m); 
	//memory allocation for dynamically storing a string of n elements
	b=(int*) malloc(m*sizeof(int));
   //introduction of elements in string
	printf ("Enter the elements in string 2: \n"); 
	for (j=0; j<m; j++)
	{
        printf ("element %d: ", j+1);
		scanf ("%d", &b[j]);
	}
//--------------------------------------------------------------------------------- 
	//display elements of two strings
	printf ("Integers elements of string 1 are : \n");
	for (i=0; i<n; i++)
	{
       printf ("element %d = %d\n", i+1, a[i]);
	} 
	printf ("Integers elements of string 2 are: \n");
	for (j=0; j<m; j++)
	{
       printf ("element %d = %d\n", j+1, b[j]);
	} 
//---------------------------------------------------------------------------------	 
	//sort string I

	do
	{
		found=0;
		for(i=0;i<n-1;i++)
		if (a[i]>a[i+1])
		{
			aux=a[i];
			a[i]=a[i+1];
			a[i+1]=aux;
			found=1;
		}
	} 
	while(found); 
	//display after sorting string 1

	printf ("String elements 1 after sorting are: \n");
	for (i=0; i<n; i++)
	{
       printf ("element %d = %d\n", i+1, a[i]);
	}
//--------------------------------------------------------------------------------- 
	//sort string II

	for(i=0;i<m-1;i++)
	for(j=i+1;j<m;j++)
	if(b[i]>b[j])
	{
		aux=b[i];
		b[i]=b[j];
		b[j]=aux;
	}
 
	//display after sorting string 2

		printf ("String elements 2 after sorting are: \n");
	for (j=0; j<m; j++)
	{
       printf ("element %d = %d\n", j+1, b[j]);
	}
//---------------------------------------------------------------------------------
	//actual merge
 
	//memory allocation for dynamically storing a string of n elements
	c=(int*) malloc((n+m)*sizeof(int)); 
	i=j=k=0;
	while(i<m && j<n)
if (a[i]==b[j])	/*where we have common elements in the two vectors 
				once they are placed in the vector merge*/
	{
		c[k++]=a[i++];
		j++;
	}
	else 
	if (a[i]<b[j]) c[k++]=a[i++];
	else c[k++]=b[j++]; //or {c[k]=b[j]; k++; j++;}

	if(i<m) //the case remains unnoticed elements in "a" copy to "c"
		for(j=i;j<n;j++)
			c[k++]=a[j];
	else //the case remains unnoticed elements in "b" copy to "c"
		for(i=j;i<m;i++)
			c[k++]=b[i]; 
	//display after sorting strings

		printf ("After sorting string elements are: \n");
	for (k=0; k<(m+n); k++)
	{
       printf ("element %d = %d\n", k+1, c[k]);
	} 
//---------------------------------------------------------------------------------

getchar();
int var;
scanf("%d", &var);
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.