Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

How to pass one dimensional array to function in c.

How to pass one dimensional array to function in c programming.



#include <stdio.h>
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main(){
int a[N];
printf("Input data into the matrix:\n");
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}

void fstore1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}

void fretrieve1D(int a[], int n){
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}

void fedit1D(int a[], int n){
int i, q;
for ( i = 0; i < n; ++i ){
printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("Enter new value: ");
scanf("%d", &a[i]);
}
}
}

Write a c program which passes two dimension array to function.

How To Write a c program which passes two dimension array to function.


#include <stdio.h>
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main(){
  int a[M][N];
  printf("Input data in matrix (%d X %d)\n", M, N);
  fstore2D(a);
  fretrieve2D(a);
  return 0;
}
void fstore2D(int a[][N]){
    int i, j;
    for (i = 0; i < M; ++i){
    for (j = 0; j < N; ++j)
         scanf("%d", &a[i][j]);
    }
}
void fretrieve2D(int a[][N]){
   int i, j;
   for ( i = 0; i < M; ++i ){
       for ( j = 0; j < N; ++j)
            printf("%6d ", a[i][j]);
       printf("\n");
   }
}

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.

Solution For C Program - 

Write overloaded function templates for finding the roots of the linear (a * x + b = 0) and square (a * x2 + b * x + c = 0) uravneniy.Zamechanie: in function to send coefficients of the equations.


#include <iostream>
#include <math.h>
#include <windows.h>

using namespace std;

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);



double findUnknown(int, int);
double findUnknown(double, double);

double findUnknown(int, int, int);
double findUnknown(double, double, double);

void testFindUnknow2IntType(int test, int a, int b , double expected)
{
double result = findUnknown(a,b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow2DoubleType(int test, double a, double b, double expected)
{
double result = findUnknown(a, b);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3IntType(int test, int a, int b, int c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}

void testFindUnknow3DoubleType(int test, double a, double b, double c, double expected)
{
double result = findUnknown(a, b, c);

if (expected == result)
{
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
cout << test << " passed\n";
}
else
{
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
cout << test << " failed\n";
cout << result << " actual\n";
cout << expected << " expected\n\n";
}
}


void main()
{
int menuButton = 0;
int show = 0;

int a = 0;
int b = 0;
int c = 0;

cout << "1.Line\n2.Square\n";
cin >> menuButton;

if (menuButton == 1)
{
/*cout << "1. a*x + b = 0" << endl;
cin >> a >> b;

cout << findUnknown(a, b) << endl;*/

testFindUnknow2IntType(1, 2, 4, -2);
testFindUnknow2IntType(2, 1, 5, -5);
testFindUnknow2DoubleType(3, 4, 2, 0.5);
testFindUnknow2DoubleType(4, 5, 2.5 , 0.5);

}

if (menuButton == 2)
{
/*cout << "2. a*x^2 + b*x + c = 0" << endl;
cin >> a >> b >> c;

show = findUnknown(a, b, c);
cout << "X2 = " << show << endl;*/

testFindUnknow3IntType(1, 1, -2, -3, -1);
testFindUnknow3IntType(2, 1, 5, 6, -3);
testFindUnknow3DoubleType(3, 0.5, 6, 0, -12);
testFindUnknow3DoubleType(4, 12, 5, -0.5, -0.5);

}
}


double findUnknown(int a, int b)
{
double x = b * (-1) / a;
return x;
}

double findUnknown(double a, double b)
{
double x = b / a;
return x;
}

double findUnknown(int a, int b, int c)
{
int discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / 2 * a;
//double x2 = (b*(-1) - sqrt(discriminant)) / 2 * a;

return x1;
}

double findUnknown(double a, double b, double c)
{
double discriminant = b*b - 4 * a * c;

double x1 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );
//double x2 = (b*(-1) - sqrt(discriminant)) / ( 2 * a );

return x1;
}

C Program Function Example

How to write a C Program Function Example in C Programming Language ?


Solution For C Program :

Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.

Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.

Write a function that calculates the geometric function in an array NxM elements from the elements with values ​​even located under the main and over the opposite diagonal .



Solution For C Program :

// Napisać funkcję obliczającą funkcję geometryczną w tablicy NxM elementowej z elementów o wartościach parzystych znajdujących się pod główną i ponad przeciwną przekątną.

float average (int tab[N][M])
{
int i,j,iloczyn=1;
for(i=0;i<N;i++)
   for(j=0;j<M;j++)
      if(i>j && i<(N-1)-j && i&1==0)
        iloczyn*=tab[i][j];
return pow(iloczyn, (1/(N*M)));
}

C Program To Find LCM and HCF Of Two Number Using Function - 2

How to write a C Program To Find LCM and HCF Of Two Number Using Function in C Programming Language ?


Solution For C Program :

/*C Program To Find LCM and HCF Of Two Number Using Function.*/
#include<stdio.h>
#include<conio.h>
int lcm(int a,int b);
int hcf(int a,int b);
void main()
    {
    int a,b,ch,z;
    char choice='y';
    while(choice=='y' || choice=='Y')
       {
       printf("1.LCM");
       printf("\n2.HCF");
       printf("\nEnter your choice:=");
       scanf("%d",&ch);
       if(ch==1 || ch==2)
          {
          printf("\nEnter first number:=");
          scanf("%d",&a);
          printf("\nEnter second number:=");
          scanf("%d",&b);
          }
       switch(ch)
          {
          case 1:z=hcf(a,b);
             printf("LCM:=%d",z);
             break;
          case 2:
             z=lcm(a,b);
             printf("HCF:=%d",z);
             break;
          default:
             printf("Wrong choice");
          }
          fflush(stdin);
          printf("\nDO YOU WANT TO CONTINUE Y/N:=");
          scanf("%c",&choice);
       }
    }
int lcm(int a,int b)
    {
    int lcm,c;
    while(a!=b)
       {
       if(a>b)
          {
          c=a-b;
          a=c;
          lcm=c;
          }
       if(b>a)
          {
          c=b-a;
          b=c;
          lcm=c;
          }
       }
       return(lcm);
    }
int hcf(int a,int b)
    {
    int hcf,c,a1,b1;
    a1=a;
    b1=b;
    c=lcm(a,b);
    hcf=(b1*a1)/c;
    return(hcf);
    }

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

C Program To Convert Temperature In Celsius To Fahrenheit, Using Function

How to write a C Program/Code To Convert Temperature In Celsius To Fahrenheit, Using Function in C Programming Language ?


Solution For C Program : 

/*C Program To Convert Temperature In Celsius To Fahrenheit, Using Function*/

#include <stdio.h>
void main()
{
void Fahrenheit(float c);
float c;
printf("\nEnter the Temperature in Celsius : ");
scanf("%f", &c);
Fahrenheit(c);
}
void Fahrenheit(float c)
{
printf("\nThe Temperature %0.2f Celsius = %0.2f Fahrenheit.", c, ( 9 * c) / 5 + 32);
return;
}


You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable