Computer Methods in Science Experiments
Friday, November 1, 2024
Array Occupancy Manager
using namespace std;
// Function to store a number in an unoccupied array element
void storeNumber(int arr[], bool occupied[], int index, int number) {
// Check if the array element at 'index' is unoccupied
if (!occupied[index]) {
arr[index] = number; // Store the number at the specified index
occupied[index] = true; // Mark the index as occupied
} else {
cout << "Error: Array element at index " << index << " is already occupied." << endl;
}
}
// Function to take a number from an occupied array element
int takeNumber(int arr[], bool occupied[], int index) {
// Check if the array element at 'index' is occupied
if (occupied[index]) {
return arr[index]; // Return the number stored at the specified index
} else {
cout << "Error: Array element at index " << index << " is unoccupied." << endl;
return -13; // Return -13 if the element is unoccupied
}
}
// Function to clear the stored number from an occupied array element
void clearNumber(int arr[], bool occupied[], int index) {
// Check if the array element at 'index' is occupied
if (occupied[index]) {
arr[index] = 0; // Clear the number (optional, as occupied[] will indicate emptiness)
occupied[index] = false; // Mark the index as unoccupied
} else {
cout << "Error: Array element at index " << index << " is already unoccupied." << endl;
}
}
// Function to return the largest value stored in the occupied array elements
int getMax(int arr[], bool occupied[], int size) {
int max = arr[0];
bool found = false; // Flag to check if we have at least one occupied element
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Only consider occupied elements
if (!found || arr[i] > max) {
max = arr[i];
found = true;
}
}
}
if (!found) {
cout << "No occupied elements in the array." << endl;
return -13;
}
return max;
}
// Function to return the smallest value stored in the occupied array elements
int getMin(int arr[], bool occupied[], int size) {
int min = arr[0];
bool found = false; // Flag to check if we have at least one occupied element
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Only consider occupied elements
if (!found || arr[i] < min) {
min = arr[i];
found = true;
}
}
}
if (!found) {
cout << "No occupied elements in the array." << endl;
return -13;
}
return min;
}
// Function to calculate and return the average of numbers in occupied array elements
double getAverage(int arr[], bool occupied[], int size) {
int sum = 0;
int count = 0;
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Only sum up occupied elements
sum += arr[i];
count++;
}
}
if (count == 0) {
cout << "No occupied elements to calculate average." << endl;
return -13;
}
return (1.0 * sum) / count; // Return the average as a double
}
// Function to report the total number of occupied array elements
int getOccupiedCount(bool occupied[], int size) {
int count = 0;
for (int i = 0; i < size; i++) {
if (occupied[i]) { // Count occupied elements
count++;
}
}
return count;
}
// Main function to test the functions
int main() {
const int SIZE = 100; // Define the size of the array
int arr[SIZE] = {0}; // Array to store integers, initialized to 0
bool occupied[SIZE] = {false}; // Boolean array to track occupied elements
// Test cases
storeNumber(arr, occupied, 10, 5); // Store 5 in index 10
storeNumber(arr, occupied, 20, 3); // Store 3 in index 20
storeNumber(arr, occupied, 30, 4); // Store 4 in index 30
storeNumber(arr, occupied, 10, 6); // Attempt to store 6 in index 10 (should fail)
cout << "Take number at index 10: " << takeNumber(arr, occupied, 10) << endl;
cout << "Take number at index 40 (unoccupied): " << takeNumber(arr, occupied, 40) << endl;
clearNumber(arr, occupied, 20); // Clear number at index 20
cout << "Take number at index 20 (after clearing): " << takeNumber(arr, occupied, 20) << endl;
cout << "Max value: " << getMax(arr, occupied, SIZE) << endl;
cout << "Min value: " << getMin(arr, occupied, SIZE) << endl;
cout << "Average value: " << getAverage(arr, occupied, SIZE) << endl;
cout << "Total occupied elements: " << getOccupiedCount(occupied, SIZE) << endl;
return 0;
}
Saturday, October 26, 2024
Three-Body Gravitational Simulation
Three-Body Gravitational Simulation
Friday, December 15, 2023
Create a C++ program for Visual Studio
Creating a full C++ graphical program to run in Visual Studio without the use of external graphics packages is quite extensive and typically requires a library such as the Simple and Fast Multimedia Library (SFML), OpenGL, or SDL which need to be installed separately.
However, Visual Studio includes the GDI+ graphics library by default, which can be used for basic drawing tasks. Here is a sketch of what the C++ code might look like using GDI+ for drawing. Note that GDI+ is a part of the Windows API, and this will only work on Windows systems.
Please note that this code is a simplified example and assumes the "coordinates.txt" file is formatted correctly with x y
coordinates on each line. You'd need to create a Windows project in Visual Studio and link to the Gdiplus library for this code to compile and run.
Additionally, proper error checking, file existence checks, and GDI+ initialization checks should be added for a robust application.
To run the provided C++ program using GDI+ for drawing in Visual Studio, follow these steps:
Open Visual Studio: Start Visual Studio and create a new project by selecting "Create a new project."
Choose Project Type: In the new project dialog, choose "Windows Desktop Wizard" and click "Next."
Configure the Project: Give your project a name and location, and click "Create."
Windows Desktop Wizard: Choose "Windows application" and make sure "Empty project" is unchecked. Click "Finish."
Add a New Item: Right-click on the "Source Files" folder in the Solution Explorer, select "Add," and then "New Item."
Create a C++ File: Choose "C++ File (.cpp)" and give it a name, then click "Add."
Copy and Paste Code: Copy the provided C++ code into the new .cpp file you just created.
Add GDI+ Library: Right-click on your project in Solution Explorer, choose "Properties," navigate to "Linker -> Input," and under "Additional Dependencies," add
gdiplus.lib
.Initialize GDI+: Make sure the code includes the
GdiplusStartupInput
andGdiplusShutdown
functions to properly initialize and shut down GDI+.Create Coordinates File: Create a text file named "coordinates.txt" with the XY coordinates, and place it in the same directory as your executable, usually under the "Debug" or "Release" folder inside your project directory.
Build the Project: Press "Ctrl+Shift+B" to build the project.
Run the Program: Once the build is successful, press "F5" to run the program.
View the Output: If everything is correct, a window should appear displaying the points and polyline as specified in your "coordinates.txt" file.
Make sure to check for any compilation errors and address them as needed. If you encounter any issues, consult the error messages and Visual Studio's help resources for troubleshooting.
If you're having trouble finding where to add gdiplus.lib
in Visual Studio, follow these steps:
Open Project Properties: Right-click on your project in the Solution Explorer (the project, not the solution or individual files) and select "Properties" from the context menu.
Navigate to Linker Settings: In the Properties window, go to the "Linker" section. You might see several subsections under "Linker."
Select Input: Click on the "Input" subsection under "Linker."
Add Additional Dependencies: In the "Input" section, find the "Additional Dependencies" field. Here, you can add
gdiplus.lib
. It might be a semicolon-separated list, so if there are other dependencies already listed, just add a semicolon (;
) followed bygdiplus.lib
.Apply and OK: After adding
gdiplus.lib
, click "Apply" and then "OK" to save your changes.
If "Linker" or "Input" is not visible, ensure that your project is a C++ Windows project and not a different type. If you still can't find it, you may need to consult the specific documentation for your version of Visual Studio or seek help from Visual Studio's support resources.
Saturday, June 24, 2023
Friday, March 31, 2023
🔭 Tank Tops Newest: Text Prompts "cos(y)" Initial Resolution Thumb Runtime Short Seed 1748675345 Overall Prompt Weight 50% Model SDXL BETA Sampling method K_LMS Racerback Tank Top
Tank Tops
Array Occupancy Manager
#include <iostream> using namespace std; // Function to store a number in an unoccupied array element void storeNumber(int arr[], bool...
-
#include <iostream> using namespace std; // Function to store a number in an unoccupied array element void storeNumber(int arr[], bool...
-
Tank Tops 11,468 Results Most Relevant Trending Newest Best Selling Text Prompts "cos(y)" Initial Resolution Thumb Runtime Short S...
-
Create a C++ program for Visual Studio that inputs a file with coordinates on the XY plane of N points, and then displays the location of th...