C ++
1. Write down the missing code according to the comments.
|
#include <cstdlib> #include <iostream> using namespace std; void fun1(int radius) { /* dynamic memory management */ double * dmptr; double circumference;
/* add the code below to: - allocate a memory location for a double and nameless variable in heap and assign its address to the pointer - assign 3.14 to the variable in heap via the pointer. */
/* add the code below to: - calculate the circumference of a circle. Its radius is from the parameter of this function and the value of pi is from the nameless variable in heap */
/* print the area */ cout<<" circumference = "<< circumference <<endl;
delete dmptr; } void fun2(int n) { int sum = 0; int *dmptr;
/* add the code below to: allocate n memory locations for an integer and nameless array in heap via a pointer */ /* add the code below to: inializate the array in heap */ for(int i=0; i<n; i++) { dmptr[i] = i * 2; }
/* calculate the sum of the elements of the array in heap */ /* print the sum */ cout<<"sum="<<sum <<endl;
delete[] dmptr; } int main() { fun1(2); fun2(5);
return 0; } |
//################## PGM START ##########################################
#include <cstdlib>
#include <iostream>
using namespace std;
void fun1(int radius){
/*
dynamic memory management
*/
double * dmptr;
double circumference;
/*
add the code below to:
- allocate a memory location for a double and
nameless variable in heap and assign its address to the
pointer
- assign 3.14 to the variable in heap via the
pointer.
*/
dmptr=new double();
*dmptr=3.14;
/*
add the code below to:
- calculate the circumference of a circle. Its
radius is from the parameter of this function and the value of pi
is from the nameless variable in heap
*/
circumference=2*(*dmptr)*radius;
/*
print the area
*/
cout<<" area =
"<<(*dmptr*radius*radius)<<endl;
cout<<" circumference = "<<
circumference <<endl;
delete dmptr;
}
void fun2(int n){
int sum = 0;
int *dmptr;
/* add the code below to:
allocate n memory locations for an integer
and nameless array in heap via a pointer
*/
dmptr=new int[n];
/* add the code below to:
inializate the array in heap
*/
*dmptr={0};
for(int i=0; i<n; i++) {
dmptr[i] = i *
2;
}
/*
calculate the sum of the elements of the array
in heap
*/
for(int i=0;i<n;i++){
sum+=dmptr[i];
}
/*
print the sum
*/
cout<<" sum="<<sum <<endl;
delete[] dmptr;
}
int main(){
fun1(10);
fun2(5);
return 0;
}
//############################### PGM END ####################################
OUTPUT
###########

C ++ 1. Write down the missing code according to the comments. #include <cstdlib> #include <iostream>...
Who could write the array.cpp file ? //main.cpp #include "array.hpp" #include <iostream> int main() { int n; std::cin >> n; array a(n); for (int i = 0; i < n; i++) { std::cin >> a.data()[i]; } std::cout << "array size:" << a.max_size() << std::endl; std::cout << "array front:" << a.front() << std::endl; std::cout << "array back:" << a.back() << std::endl; int* data = a.data(); std::cout << "array elements using data:" << std::endl; for (int i = 0; i < n;...
Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...
Consider the following C++code snippet and what is the output of this program? # include<iostream> using namespace std; void arraySome (int[), int, int); int main () const int arraysize = 10; int a[arraysize]-1,2,3,4,5, 6,7,8,9,10 cout << "The values in the array are:" << endl; arraySome (a, 0, arraySize) cout<< endl; system ("pause") return 0; void arraySome (int b[], int current, int size) if (current< size) arraySome (b, current+1, size); cout << b[current] <<""; a) Print the array values b) Double...
The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...
THE CODE CALCULATES THE MEAN MEDIAN AND MODE OF THE SET OF NUMBERS THE CODE IS WRITTEN IN C++ PLEASE FIX THE MODE FUNCTION ! SO THAT IT OUTPUTS 0 WHEN THERE ARE NO REPETITIONS IN THE DATASET eg. for 1,2,3,4,5 mode should be zero but it gives 1 ! #include<iostream> #include<math.h> using namespace std; class statistical{ protected: double *dataArray; int size; public: statistical(double a[], int s){ dataArray = new double[s]; for (int i = 0; i<s; i++){ dataArray[i] =...
moviestruct.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct{
int id;
char title[250];
int year;
char rating[6];
int totalCopies;
int rentedCopies;
}movie;
int loadData(ifstream &infile, movie movies[]);
void printAll(movie movies[], int count);
void printRated(movie movies[], int count);
void printTitled(movie movies[], int count);
void addMovie(movie movies[],int &count);
void returnMovie(movie movies[],int count);
void rentMovie(movie movies[],int count);
void saveToFile(movie movies[], int count, char *filename);
void printMovie(movie &m);
int find(movie movies[], int...
--C++-- --spc16-7.cpp-- // Chapter 16, Programming Challenge 7: TestScores class #include <iostream> #include "TestScores.h" #include "NegativeScore.h" using namespace std; int main() { // Constant for the number of test scores const int NUM_SCORES = 5; try { // Create an array of valid scores. int myScores[NUM_SCORES] = { 88, 90, 93, 87, 99 }; // Create a TestScores object. //TestScores myTestScores(myScores, NUM_SCORES); // optional constructor TestScores myTestScores(myScores);...
Arrays C++ #include <iostream> using namespace std; int main() { int tests[6]; // array declaration int sum = 0; float avg; //input test scores cout << " Enter " << 6 << " test scores: " << endl; for (int i = 0; i < 6; i++) { cout << "Enter Test " << i + 1 << ": "; cin >> tests[i]; } return 0; } Type...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
The code should be written in C++. I included the Class used for this function. Also, please fix the main function if necessary. Description: This function de-allocates all memory allocated to INV. This will be the last function to be called (automatically by the compiler) //before the program is exited. This function also erased all data in the data file. There are other functions that add, process and alter the file; the file printed after those functions will have new...