Question

In: Computer Science

Write a main function that reads a list of integers from a user, adds to an...

Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array.

Requirement: Using pointer notation.

Please do this with C++

Solutions

Expert Solution

Code:

#include<iostream>
using namespace std;
int main()
{
   /*Main function*/
   int n,i,larg;/*Declaring variables*/
   cout<<"Enter no of elements in the array: ";
   cin>>n;
   /*Reading size of the array from the user*/
   int *array=new int(n);
   /*declaring the dynamic array*/
   cout<<"Enter elements of the array:"<<endl;
   /*Asking user to enter the elements*/
   for(i=0;i<n;i++)
   {
       cin>>*(array+i);
       /*Here we read elements formt he user*/
       if(i==0)
       {
           /*If it is the first element we make first element as larg*/
           larg=*(array+0);
       }
       else
       {
           if(larg<*(array+i))
           {
               /*here we compare larg with the entered element
               if it is larger then we make larg as the new element*/
               larg=*(array+i);
           }
       }
   }
   cout<<"\nLargest element is: "<<larg;
   /*Finally we print the largest element of the array*/
}

Output:

Indentation:


Related Solutions

C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
1. Write a function named “Number” that reads in a list of numbers until the user...
1. Write a function named “Number” that reads in a list of numbers until the user enters 0. It will return true if the user has entered more even numbers than odd numbers; otherwise it returns false. 2. Write a code segment to sort an array of students in ascending order of their IDs. Assume the array has been filled with names and assume the following declaration: struct Student { string name; int ID; } Student roster[30]; // Code to...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Write an application that reads three integers, adds all three together and computes an average of...
Write an application that reads three integers, adds all three together and computes an average of the three entries and computes any remainder of the integer division. A remainder represents the modulus result of dividing an even by an odd number or vice versa. Display the output. Enter an integer score 3 Enter an integer score 6 Enter an integer score 4 The average of 3, 6, 4 is 4 with a remainder of 1 Press any key to continue...
Design and implement a program that reads a series of 10 integers from the user and...
Design and implement a program that reads a series of 10 integers from the user and prints their average. Read each input value as a string, and then attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException (meaning that the input is not a valid number), print an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
1. Design and implement a program that reads a series of integers from the user and...
1. Design and implement a program that reads a series of integers from the user and continually prints their average after every reading. The input stops when the user enters “stop” as the input value. Read each input value as a string, then check if it is “stop” or not. If the string is not “stop”, then, attempt to convert it to an integer using the Integer.parseInt method. If this process throws a NumberFormatException, meaning that the input is not...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
1. Write an assembly language program that prompts the user for and reads four integers (x1,...
1. Write an assembly language program that prompts the user for and reads four integers (x1, y1, x2, y2) which represent the coordinates of two points. Make sure you keep track of which number is which. 2. Treat the line between the points as the radius of a sphere and compute the surface area of the sphere. Print the output with a label, such as “The surface area of the sphere is: …”. Hint: The distance between the points is...
Write a C++ function called parse that reads one line of user input from the keyboard...
Write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input.  Your function should be passed the array and a reference variable that is to be assigned the length of the array.  Prompt the user and read the input from within the function. For example:  If the user inputs copy this that, the resulting array would have length 3 and contain the strings “copy”, “this”, and “that”....
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and...
8.26 LAB: Output numbers in reverse Write a program that reads a list of integers, and outputs those integers in reverse. The input begins with an integer indicating the number of integers that follow. For coding simplicity, follow each output integer by a space, including the last one. Assume that the list will always contain fewer than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: 10 8 6 4 2 To achieve...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT