Question

Description An array in C++ is a collection of items stored at contiguous memory locations and...

Description

An array in C++ is a collection of items stored at contiguous memory locations and elements can be accessed randomly using indices of an array. They are used to store similar type of elements as in the data type must be the same for all elements. One advantage of arrays is easy data manipulation and accessibility of elements stored in consecutive locations.

The Problem:

One teaching assistant of a computer science department in Engineering University got a simple question to solve within time but he is little busy in his new job, so he asked you to solve this problem. He has an array of positive integers A1,A2,...,An with length N and he wants you to print an array of the same length (N) where the values in the new array are the sum of every number in the array, except the number at that index.



Input Format:



First line of input contains N which is the length of the array and second line contains the elements of the array separated by a space.



Enter size of Array:

6

Enter elements of array separated by space:

3 9 2 5 17 25


Output Format:

The program should display a transformed array where each element of the array is the sum of all other elements except the one on that index.



Transformed array:

58 52 59 56 44 36


0 0
Add a comment Improve this question Transcribed image text
Answer #1

explnation:

here is first we are reading values into the array then finding the sum of all elements in array then in the new array we are storing the values excluding index value from the sum.

code:

#include <iostream>
using namespace std;
int main()
{
int N,sum=0; //declaring varibles
cout<<"Enter size of Array \n";
cin>>N; //reading the size of array
int a[N]; //declaring array with specifed size
int b[N]; //declaring new array for holding the Transformed array values
cout<<"enter array elements:\n";
for(int i=0;i<N;i++)
cin>>a[i]; //this is for loop to read values
for(int i=0;i<N;i++)
sum=sum+a[i]; //to find the sum of all elements in array
for(int i=0;i<N;i++)
b[i]=sum-a[i]; //storing array values excluding index value
cout<<"Transformed array:\n";
for(int i=0;i<N;i++)
cout<<b[i]<<" "; //displaying output
return 0;
}

OUTPUT

Enter size of Array
6
enter array elements:
3 9 2 5 17 25
Transformed array:
58 52 59 56 44 36

screenshot:

1 #include <iostream> 2 using namespace std; 3 int main() int N, sum=0; //declaring varibles cout<<Enter size of Array \n;

Add a comment
Know the answer?
Add Answer to:
Description An array in C++ is a collection of items stored at contiguous memory locations and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Question 9 12 pts Let's assume that charlist() is an array of 10000 strings stored in...

    Question 9 12 pts Let's assume that charlist() is an array of 10000 strings stored in the array in the increasing order of their length (shortest string is stored at index 0 and longest one is store at index 9999). Write an efficient function with the following signature that gets the list of strings as its first input parameter and returns a binary number (0 or 1) indicating whether the list contains a string of length n where n is...

  • Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...

    Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...

  • /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /**...

    /** * A collection of methods related to multi-dimensional arrays. */ public class Array2Lab { /** * Return whether k is in list. * Precondition: the elements of list are not null. * @param list the array to be searched. * @param k the number to search for. * @return true if k is an element of list, and false otherwise. */ public static boolean contains(Object[][] list, Object k) { return false; }    /** * Create a String that...

  • Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1...

    Consider a non-empty int array ints. A contiguous subarray ints[ start .. start + len -1 ] (with starting index start and length len) is called a flat if all elements of that subarray are equal. Furthermore, such a subarray is called a plateau if it is flat and each of the elements ints[start -1] and ints[start + len] that immediately proceed/succeed the subarray are either nonexistent (i.e., out of array’s index range) or are strictly smaller than the elements...

  • Plz i want answer these question using list in python programme. You are given two sequences...

    Plz i want answer these question using list in python programme. You are given two sequences of n integers: 21, 22, ...,an and b1,b2, ..., bn Print a sequence of 2n integers created by alternating elements from the given sequences: a1, 61, 42, 62, a3, 63, ..., an, bn. Input The first line contains a positive integer n (1 <n<1000) - the length of the sequences The second line contains n space-separated integers 01, 02, ..., an (-1000 <a; <...

  • Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring...

    Lab Topics • The basics of Array object Use the following Coding Guidelines • When declaring a variable, you usually want to initialize it. Remember you cannot initialize a number with a string. Remember variable names are case sensitive. Use tabs or spaces to indent code within blocks (code surrounded by braces). Use white space to make your program more readable. Use comments after the ending brace of classes, methods, and blocks to identify to which block it belongs. Problem...

  • In Java, Implement a class MyArray as defined below, to store an array of integers (int)....

    In Java, Implement a class MyArray as defined below, to store an array of integers (int). Many of its methods will be implemented using the principle of recursion. Users can create an object by default, in which case, the array should contain enough space to store 10 integer values. Obviously, the user can specify the size of the array s/he requires. Users may choose the third way of creating an object of type MyArray by making a copy of another...

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

  • programing C,already write part of code (a) Write a function print.array that receives an array of...

    programing C,already write part of code (a) Write a function print.array that receives an array of real numbers and the number of el stored in the array. This function must display the elements of the array in order with each one on a line by itself in a field width of 7 with two decimal places. (See sample output. Recall the format specifier would be "%7.21f"). (b) Sometimes we want to treat an array as a mathematical vector and compute...

  • This is being done in c using command line in Linux 1.1 llist: Linked list Write...

    This is being done in c using command line in Linux 1.1 llist: Linked list Write a program llist that maintains and manipulates a sorted linked list of integers according to instructions received from standard inpu. The linked list is maintained in order, meaning that each integer will be larger than the one preceeding it. 1list maintains a linked list containing zero or more integers, ordered from least to greatest llist will need to allocate space for new nodes as...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT