home Sandy, UT
Wednesday, 2017.11.22
04:14 MDT [GMT-7]
Brionews - Company Logo Condition: Partly Cloudy
Temperature: 4.3°C (39.7°F)
Barometer: 1029 mb and rising
IT
MIX
News
Low Cost Affordable C C++ PHP mySQL Perl Programs Development
Assignment for Discrete Math class, short for Discrete Structures.
Given an equation with n variables whose sum is a constant k, compute the number of possible solutions.
Goes solved through factorials.
Any variable can be zero but not negative.
Warning: I am not sure it's bugs-free.
Download the executable: constant.exe
October 28, 2004

n-solutions with m-variables in a Costant Sum

/**
  *  @title: hw4.cpp
  *  @date: 2004.10.28 - 21:25
  *  @author: vinnie
  *  @source for hw4.exe
  */

#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

long double fact( long double  x)
{
  if ( x > 0) return x * fact(x-1);
  return 1;
}

void main( )
{
int iNVars, iConstSum;
char strNVars[10], strConstSum[10];
char c;

//   code
system("color 1f");

// menu' label
MYMENU:   //  begin

//  reset vars
iNVars = 0;
iConstSum = 0;
memset( strNVars, '\0', 10);
memset( strConstSum, '\0', 10);

//  print intro & menu
system("cls");
printf("\t\t        SLCC - CS 2310-01 - Fall '04 - HW4\n");
printf("\n Given a equation with n nonnegative integers,");
printf("\n whose sum is k, list all the solutions.\n");

printf("\n Please input how many variables : ");
gets( strNVars);
iNVars = atoi( strNVars);

printf(" Please input the constant sum   : ");
gets( strConstSum);
iConstSum = atoi( strConstSum);
//////////////////////////////////////////////////////////////

int den = iNVars + iConstSum - 1;
long double res = fact( den) / ( fact( iConstSum) * fact( iNVars - 1));

printf("\n %Le solutions\n", res);
//////////////////////////////////////////////////////////////

//  ask for an other trip
printf("\n Another one [Y/y/N/n]? ");
while ( !kbhit() );
c = getchar( );
if ( c == 'Y' || c == 'y' ) goto MYMENU;
//////////////////////////////////////////////////////////////

printf("\n Thanks for using HW4\n");
printf(" Vincenzo Maggio Code - released under GPL\n");
system("color 0f");
}

2004.10.28

Vincenzo Maggio