# Data Structure and Algorithm

#### menguin

##### Geek
Pinoy Techie
1. This is a C++ Program to emulate N-dice roller. This can be done by generating random number between 1-6.
Here is source code of the C++ Program to Emulate N Dice Roller. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
Code:
``````#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
cout << "Enter the number of dice: ";
int n;
cin >> n;
cout << "The values on dice are: ( ";
for (int i = 0; i < n; i++)
cout << (rand() % 6) + 1<<" ";
cout<<")";
}`````` 2. C Program to Calculate Sum & Average of an Array
This C Program calculates the sum & average of an array. It declares an array and then add the array elements and finds the average of the array.

Here is source code of the C program to calculate the sum & average of an array. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
Code:
``````  /*
* C program to read N integers into an array A and
* a) Find the sum of negative numbers
* b) Find the sum of positive numbers
* c) Find the average of all numbers
* Display the results with suitable headings
*/
#include  <stdio.h>
#define MAXSIZE 10

void main()
{
int array[MAXSIZE];
int i, num, negative_sum = 0, positive_sum = 0;
float total = 0.0, average;

printf ("Enter the value of N \n");
scanf("%d", &num);
printf("Enter %d numbers (-ve, +ve and zero) \n", num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements \n");
for (i = 0; i < num; i++)
{
printf("%+3d\n", array[i]);
}
/*  Summation starts */
for (i = 0; i < num; i++)
{
if (array[i] < 0)
{
negative_sum = negative_sum + array[i];
}
else if (array[i] > 0)
{
positive_sum = positive_sum + array[i];
}
else if (array[i] == 0)
{
;
}
total = total + array[i] ;
}
average = total / num;
printf("\n Sum of all negative numbers =  %d\n", negative_sum);
printf("Sum of all positive numbers =  %d\n", positive_sum);
printf("\n Average of all input numbers =  %.2f\n", average);
}``````

3. C Program to Check if a given String is Palindrome
This C Program checks if a given string is palindrome. This program first performs reverse of a string. Then it checks whether the given string is equivalent to the reversed string. If they are equal then the string is called as palindrome.

Here is source code of the C program to check a given string is palindrome. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

Code:
``````  /*
* C program to read a string and check if it's a palindrome, without
* using library functions. Display the result.
*/
#include <stdio.h>
#include <string.h>

void main()
{
char string, reverse_string = {'\0'};
int  i, length = 0, flag = 0;

fflush(stdin);
printf("Enter a string \n");
gets(string);
/*  keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
for (i = length - 1; i >= 0; i--)
{
reverse_string[length - i - 1] = string[i];
}
/*
* Compare the input string and its reverse. If both are equal
* then the input string is palindrome.
*/
for (i = 0; i < length; i++)
{
if (reverse_string[i] == string[i])
flag = 1;
else
flag = 0;
}
if (flag == 1)
printf("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}``````