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.
Write two versions of a program that reads from the user a sequence of positive integers...
Write two versions of a program that reads from the user a sequence of positive integers ending with -1, and another positive integer num that the user wishes to search for. The program should then print all the line numbers in sequence entered by the user, that contain num, or a message saying that num does not show at all in the sequence. Your program should interact with the user exactly as it shows in the following example: Please enter...
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...
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, 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 integer), display an appropriate error message and prompt for the number again. Continue reading values until 10 valid integers have been entered.
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...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT