Question

In: Computer Science

Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...

Write a C++ program (using pointers and dynamic memory allocation only) to
implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise.

(2)Create an array of Planets. Populate the array and print the contents of the array
using the pointer notation instead of the subscripts.

Solutions

Expert Solution

#include <iostream>
using namespace std;

//This function takes 2 parameters one character array and search character
bool SearchParameter(char *str, char ch)
{
    //this for loop checks each of the character
    //in the character array 'str' for search character 'ch'
    for (int i = 0; i < sizeof(char *); i++)
    {
        if (ch == str[i])
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
//the main method
int main()
{
    //size to increase the dynamic array
    int size = 30;
    //to hold search parameter
    char ch;
    //dynamically allocating memory size through malloc
    char *str = (char *)malloc(sizeof(char) * size);
    //takes single search character as input
    cout << "\nEnter the single Search parameter/character you want to search : ";
    cin >> ch;
    //takes the characte string as input
    cout << "\nEnter the whole Character String: ";
    cin >> str;
    //this is the function call which stores the result
    //as either 0=false or true=1
    bool res = SearchParameter(str, ch);
    //validate result and print the result status-RB
    if (res == true)
    {
        cout << "And the result is: true";
    }
    else
    {
        cout << "And the result is: false";
    }
}

The function working and all the code working is explained in comments .


Related Solutions

Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function,...
Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function, returning a pointer by a function, dangling pointer, and memory deallocation, pointer initialization, and struct data type. Project description In this project, you will create a database of employees of an organization while meeting the requirements described below. Your program MUST NOT interact with the user to receive inputs, so that the instructor and/or the teaching assistant can save a big time in testing...
Write a program using c, c++, or java that have four dynamic memory partitions of size...
Write a program using c, c++, or java that have four dynamic memory partitions of size 100 KB, 500 KB, 200 KB, and 450 KB. The program should accept from user the number of processes and their sizes. Then output the assignment of processes using the next fit algorithm (specifying which process, if any, is block).
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your...
C++ Memory Allocation: 1) Write a C++ program that allocates static, stack, & heap memory. Your program does not need to do anything else.  Indicate via comments where memory for at least one variable in each memory area is allocated. a) Write code that allocates static memory (only include one declaration): b) Write code that allocates stack memory (only include one declaration): c) Write code that allocates heap memory (only include one declaration): 2) Edit the C++ program below to include...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following...
Don't use vectors use pointers ,classes & objects, functions and loop etc only C++ PROGRAM Following is a partial implementation of Set class. You are required to enhance and implement the following missing functions from the implementation: A) UNION B) reset C) intersection D) difference A SAMPLE driver program : int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2); Set s4 = s1.intersection(s2); Set s5 = s1.difference(s2); s3.print("s3"); s4.print("s4");...
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to...
this program is to be done in c language. Using Pointers Create a program pointerTester.c to experiment with pointers. Implement the following steps one by one in your program: YOU NEED TO ANSWER QUESTION Use printf to print your answers at the end(after 12). 1. Declare three integer variables a, b and c. Initialize them to 0, 100 and 225, respectively. 2. Print the value of each variable and its address. 3. Add the following declaration to your code: int...
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
c++ Define polymorphism. What is the benefit of using pointers with polymorphic functions?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT