C++ Programming Code Examples
C++ > Computer Graphics Code Examples
Program to Implement First Fit Decreasing for 1-D Objects and M Bins
/* Program to Implement First Fit Decreasing for 1-D Objects and M Bins
This is a C++ Program to implement First Fit Decreasing for one dimensional objects and M bins. In simple terms this is bin packing algorithm for first fit technique. */
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
void binPacking(int *a, int size, int n)
{
int binCount = 0;
int binValues[n];
for (int i = 0; i < n; i++)
binValues[i] = size;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (binValues[j] - a[i] >= 0)
{
binValues[j] -= a[i];
break;
}
}
for (int i = 0; i < n; i++)
if (binValues[i] != size)
binCount++;
cout << "Number of bins required using first fit decreasing algorithm is:"
<< binCount;
}
int* sort(int *sequence, int n)
{
// Bubble Sort descending order
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 1; j++)
if (sequence[j] < sequence[j + 1])
{
sequence[j] = sequence[j] + sequence[j + 1];
sequence[j + 1] = sequence[j] - sequence[j + 1];
sequence[j] = sequence[j] - sequence[j + 1];
}
return sequence;
}
int main(int argc, char **argv)
{
cout << "BIN - PACKING Algorithm 1D Objects(First Fit Decreasing)";
cout << "Enter the number of items in Set: ";
int n;
cin >> n;
cout << "Enter " << n << " items:";
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
cout << "Enter the bin size: ";
int size;
cin >> size;
int *sequence = sort(a, n);
binPacking(sequence, size, n);
}
In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Consider a situation, when we have two persons with the same name, jhon, in the same class. Whenever we need to differentiate them definitely we would have to use some additional information along with their name, like either the area, if they live in different area or their mother's or father's name, etc. Same situation can arise in your C++ applications. For example, you might be writing some code that has a function called xyz() and there is another library available which is also having same function xyz(). Now the compiler has no way of knowing which version of xyz() function you are referring to within your code.
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
The cin object is used to accept input from the standard input device i.e. keyboard. It is defined in the iostream header file. C++ cin statement is the instance of the class istream and is used to read input from the standard input device which is usually a keyboard. The extraction operator(>>) is used along with the object cin for reading inputs. The extraction operator extracts the data from the object cin which is entered using the keyboard. The "c" in cin refers to "character" and "in" means "input". Hence cin means "character input". The cin object is used along with the extraction operator >> in order to receive a stream of characters.
A 'stack' is a basic "data structure" and can be defined in an 'Abstract', implementation-free manner, or it can be "Generally Defined" as a Linear List of items in which all additions and
First Input the length of a side of the triangle. Then Input the 'length of another side' of the triangle, then input the angle between these sides of the triangle. The area of the Scalene
Function overloading & Operator overloading are examples of "Polymorphism". In The C++ programming In function overloading we can have more than one function by 'same name'
In C++, under the simplest form, each node is composed of data and a reference ('a link') to the next node in the sequence. This structure allows for efficient insertion or removal of the