C++ :: Calculate Sum Of Numbers In Given Positions

Dec 16, 2013

Question: Write a program that calculates sum of the numbers in the given positions.

Input specification : There will be 4 lines of data. You will be first given the size of the positions array (n). Then, the following line will have n integers which is an ordered list in increasing order and 0 < n ≤ 3000. The third line will give the size of the number array (m) where 0 < m ≤ 5000 and The last line will have m integers between -30000 and 30000.
Note: The positions start from 1 and goes until m.

Output specification : Show one integer number. Sum of the Numbers in the given Positions.

Sample Input I
5
2 5 7 9 10
10
1 8 7 5 17 15 6 7 19 12

Sample Output I
62

View 1 Replies


ADVERTISEMENT

C/C++ :: Placing Numbers In Random Positions In Matrix?

Oct 30, 2014

i have a problem with a bit of code (part of an as-yet incomplete program that creates a sort of maze with 10 roadblocks, and then finds the shortest route to the exit.

I don't know what it means, to put tags around my code, but I shall try to point out the problem bit clearly. It is not a long segment. This part is all working fine and printing the messages the user needs to see initially:

#include <stdio.h>
#include <stdlib.h>
int main() {
printf("The number -1 shall represent the position of the robot in the matrix.
"
"The number 99 shall represent the position of the exit.
"
"The number 100 shall represent all blocks.
"
"All other numbers represent the number of moves required to reach the occupied space from the robot's position.
");

Below is where is goes bad, and I really am not sure why. The program says it has stopped responding and gets grayed out, and then I get the error message, "An access violation (Segmentation fault) raised in your program.

I have tried using the debugger, and it only tells me it found 0 errors and 0 warnings.

srand(time(NULL));
int initialmatrix [8] [8];
initialmatrix [0] [7] = 99;
int numberofblocks=0;
int randomrow;

[code]....

View 5 Replies View Related

C :: Calculate Prime Numbers In Range And Return Count Of Prime Numbers

Apr 28, 2013

I wrote a program which sends a starting and ending range to other processes and the processes calculate the prime numbers in that range and return the count of prime numbers to the head process, process 0. But this is not working properly at the moment. I realize I still have to split up the range based on how many processes I have...I still have not figured out how I want to set that up. I

Code:

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
int isPrime(int num);
int main(int argc, char **argv){
}

[code]....

View 7 Replies View Related

C++ :: Using Char Instead Of Unsigned To Calculate Numbers?

Mar 10, 2014

How do you use char instead of unsigned to calculate numbers? This is using char only and nothing else.

Step 1: I ask the user to enter a number.
Step 2: User enters a number.
Step 3: Number user entered is going to be that number squared or cubed or w/e.

For example;
"Enter a number: " 3
" Number you entered multiplied four times: " 81 (Since (3)*(3)*(3)*(3) = 81)

Another example;
"Enter a number: " 5
" Number you entered multiplied four times: " 625 (Since (5)*(5)*(5)*(5) = 625)

Code:
Char num;
cout << "Enter a number";
cin >> num;
cout << "Number you entered multiplied four times: " << (num)*(num)*(num)*(num) << endl;

View 4 Replies View Related

C/C++ :: Calculate Sum And Average Of A Sequence Of Numbers

Feb 17, 2014

Write a C program that calculates the sum and average of a sequence of numbers. Assume the first number specifies the number of values remaining to be entered. If the first number is less than 1, then the program should display an "Invalid Entry ... Please enter a positive number." message.

THIS IS HOW IT SHOULD COME OUT...
Enter the number of values to process 0

Invalid Entry ... Please enter a positive number.

Enter the number of values to process 3
Enter 3 numbers:
1
2
3

Sum: 6
Avg: 2

THIS IS THE CODE I HAVE SO FAR...

#include <stdio.h>
int main(void) {
int total=0;
int howmany;
int i;
int value;

[Code] ....

View 8 Replies View Related

C++ :: Calculate Complex Numbers Through Multiplication And Addition

Feb 28, 2013

Below is a code that is used to calculate complex numbers (a+bi, where i = sqroot (-1)) through multiplication and addition.

However, on my output file, no Header is being printed; the only thing that is being printed is "8 + 7i + = "

"complex.h" is included at the end of the code.

Code:
// Trey Brumley// CMPS
// Dr. Tina Johnson
// March 1, 2013
// Program 2: Classes
// This program will demonstrate the use of classes by using a custom "complex-number" (a+bi) class.

[Code] ......

View 4 Replies View Related

C :: Program To Calculate Factorial Of Numbers - For Loop

Feb 5, 2013

I have been working on a program to calculate the factorial of numbers. Part of my code is copied and modified from the FAQ about validating numbers in user input.

I have encountered a problem with the for loop that I am using near the end of my code. No matter what I do, it seems that my loop only does the multiplication of b = a*(a-1) and then prints. For example, inputting 5 will result in a print of 20, but the factorial is 120.

Code:
int main(void) {
char buf[BUFSIZ];
char *p;
long int a;
long int b;
long int i;

printf ("Enter a number to be factorialized: ");

[Code] ....

View 5 Replies View Related

C++ :: Adding Large Numbers And Calculate Module

Sep 7, 2013

Exmaple:
112121276783621784678236478236478623784672364782367846237846782364782367846238 + 783627846782364786237846782364782367846237846782364782367846237846237864782367846238

It should be efficient and fast. It is useful to calculate the Factorial 100000, and then I calculate the number of the module.

Example:
132142344564378657834657834657863785634786578346578346785634785678653478 % 124623874

View 2 Replies View Related

C++ :: Calculate Average From File Containing Array Of Numbers?

Feb 21, 2014

I have written two separate programs; Program 1 calculates the average of an array of numbers which has been hard-coded into the program. Program 2 reads an array of numbers from a text file and displays them on the output.

I wish to combine these two programs into one by making Program 1 read the array of numbers from the file and then calculate the average using that instead of the array { 84, 92, 76, 81, 56 } as outlined below. I only wish to display the average in the output, not the number array as Program 2 does.

I have tried to do most of the work, I just need modifying the code slightly so it reads the number array from the file and calculates the average.

Program 1

#include <iostream>
#include <cmath>
#include <math.h>
#include <fstream>
#include <string>
#include <numeric>
using namespace std;
int main() {
const int nNumStudents = 5;

[Code] .....

View 7 Replies View Related

C/C++ :: Calculate Result Of 2 Numbers That User Entered

Oct 29, 2012

In this exercise, write a rational number calculator that ask the user to enter two rational number:

a/b + c/d

and produce the result:

(ad+bc)/bd

View 10 Replies View Related

Visual C++ :: Calculate Prime Numbers In Order

May 12, 2013

So if i write a Loop to calculate Prime Numbers in order, is there any way possible for this to happen in a shorter period of time.

The loop that i constructed took about 11hrs on a Win 7 2GB ram machine to calculate about 150,000 primes, could this be done any faster................

View 14 Replies View Related

C :: Possible To Switch Two Array Positions?

Feb 12, 2015

I have a fundamental question due to my lack of understanding how arrays work.Let say I have a character array (byte array).

1. do array elements have memory adresses? i guess not but ....
2. if yes is it possible to switch two array positions? For examle:

a[x] change with a[y] for x,y < |a|

as oppose to changing values assigned to individual array positions.

View 2 Replies View Related

C :: Minimum And Maximum In All Positions

Jan 4, 2015

I wrote a program to find the minimum and the maximum values from a vector. It works fine. What I'm trying to do is show the positions of said values and it's not working quite right. When I insert 4 elements: 2 0 1 3 it says:

"The min and max are 0 and 3
The position of the min is: 01
The position of the max is: 03"

What am I doing wrong? Here is the code:

Code:

#include <stdio.h>
#include <conio.h>
int main() {
int A[10], i, j, n, min, max, C[10], k=0, D[10], l=0;
printf("Insert no. of elements in vector A

[code]....

View 6 Replies View Related

C++ :: Program To Calculate Average Of 3 Numbers - Undeclared Identifier

Sep 22, 2013

Write a program that will calculate average of 3 numbers

#include <iostream>
int
main(){
// prototypes:
float add_and_div(float, float,
float);

[Code] .....

i get the following errors:

error C2065: 'cout' : undeclared identifier
error C2065: 'cin' : undeclared identifier
error C2065: 'cout' : undeclared identifier
error C2065: 'endl' : undeclared identifier

View 2 Replies View Related

C++ :: How To Calculate Certain Cells Of Numbers Instead Of Entire Text File

May 24, 2014

As you can see, the code below calculates the average, sum, number of items, etc, for a list of numbers in two separate files and compares them with one another.

e.g. we will have a text file of 10 numbers;

45
65
24
26
26
36
35
100
109
433
etc...

The problem is that the code will perform calculations on all the numbers in the txt. or csv. file. This is problematic because if there is any text in the file, e.g. headings, then the calculations will not be performed. For instance, suppose that I only wanted to include rows 5-10 in the calculations, how would I specify this in my C++ code?

#include <iostream>
#include <cmath>
#include <math.h>

[Code]....

View 3 Replies View Related

C/C++ :: Calculate Percentage Of Even Numbers Entered By User In Array

Nov 18, 2014

exercise in c: write a program which calculates % of even numbers entered by a user in an array;

1) check if user hasn't entered float value; if has issue warning;
2) give the option for user to press "stop" to exit program any time.

int c,sk[100],i,sum1=0, sum2=0, rel;
printf ("How many numbers will you enter?
");
scanf ("%d", &c);
printf ("Enter %d numbers (to exit program press - stop)
", c);

[Code] ....

View 1 Replies View Related

C :: Guess 4 Int Array With How Many Are In Right And Wrong Positions

Feb 11, 2013

For an assignment I have to create a random array of four integers, and then I have to allow someone to input up to ten guesses to guess the array in the correct order. I also need to be able to display whatever was generated by inputting -1. Finally, after every guess I have to tell the inputter how many of the guessed integers are correct and in the correct position, as well as how many integers are correct but not in the correct position.

So far I've been able to get the random array to generate properly, but inputting negative one has no effect, although if I input it four times in a row I get to my 'lose' condition. Also, it only seems to allow the user to input 4 guesses and not 10 before going straight to the 'lose' condition. I need to get these issues sorted out before I can move on to showing how many guesses are right etc....

Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time (NULL));

[Code] ....

View 3 Replies View Related

C++ :: Create A Program That Reads Numbers And Calculate Them On A Separated Subroutine

Nov 15, 2013

I need to create a program that reads some numbers, and calculate them on a separated subroutine, and the return of this subroutine must be the sum of all the numbers. I'm getting an error but I can't figure out why =/

#include <iostream>
using namespace std;
int calc(int val, int qtd){
int sum=0;
for (int cont=0; cont<qtd; cont++)
sum=sum+val;
return sum;

[Code]...

The error that I'm getting is on the line 22.

The error message: "invalid conversion from 'int*' to 'int' [fpermissive]

View 4 Replies View Related

C++ :: Calculate Average Of A Stream Of Positive Numbers - Loop Program?

Feb 25, 2013

Write a program that calculates the average of a stream of positive numbers. The user can enter as many positive numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, treat zero as a positive number, i.e., zero counts as a number that goes into the average. Of course, the negative number should not be part of the average. You must use a function to read in the numbers, keep track of the running sum and count, compute the average, and return the average to the main() function. Note that you must use a loop to do this, as you don't know how many numbers the user will enter ahead of time. I am having problem writing a loop program for it..

View 1 Replies View Related

C++ :: User Input - Calculate Sum Of Only Positive Values While Ignoring Negative Numbers

Jun 19, 2014

So I have to make a program that allows the user to enter both positive and negative numbers and the program is suppose to calculate the sum of only the positive values while ignoring the negative values. Also it is to be a sentinel-controlled loop with a number ending the set of values.

View 4 Replies View Related

C++ :: Read List Of Random Numbers From Input File And Calculate Statistics?

Nov 11, 2013

I need my program to read a list of numbers from and input file, random.txt, and calculate the following statistics on those numbers:

A. The number of numbers in the file.
B. The sum of all the numbers in the file.
C. The average of all the numbers in the file.
D. The largest number in the file.
E. The smallest number in the file.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

[Code]......

This is what I have so far but I'm not sure if it is right. I can't get the input file to open.

View 3 Replies View Related

C++ :: Program To Swap Positions Of Digits Of User

Oct 11, 2014

I'm using code blocks ....

1.Write a program to swap positions of digits of a user entered three-digit integer N, where N is equal or between 101 and 999. (i.e. if user enters 389 your program should print 983. If user enters 300 program should print 003). Repeatedly ask user for correct N, if he/she enters an integer N which is not in the range.

2. Given that y= 4*( 1- 1/3 + 1/5- 1/7+ 1/9-...plus or minus 1/N) Write a program using a for-loop or a while-loop to compute and print the sum of first 50 terms of y.

3. a) Write a user-defined function funGx to compute G(x), where

5 if x<-10

x^2 +(5/x) if -10 <=x<-5

x^2 - (5/x-5) if -5<=x<5

x^2 -(5/x) if 5<=x<10

-5 if x>=10

b) Call the user-defined function funGx in main function to compute and print G(x)values for x= -15.5 , x=5, and x= 0.5 in an informative sentence.

View 3 Replies View Related

C++ :: User Input Two Numbers - Calculate Square Root Then Roots Are Added Up To Return Sum

Aug 23, 2014

I have tried writing a code which takes two numbers from the user and calculates their square root then the roots are added up to return the sum. The program is coming out with loads of errors.

#include<iostream>
#include<cmath>
float main(){
using namespace std;
float m1,m2,m3,m4,m5;

[Code] ....

View 4 Replies View Related

C :: How To Rearrange Structure Element On The Basis Of Positions Given In Other Array

Apr 8, 2013

I want to rearrange the positions of structure elements on the bases of perm_array.

Code:
typedef struct gg{
element d;
int group;
} gg;
gg col_data[16];

[Code] ....

How can i rearrange structure element like {ptr+2,ptr+3,ptr+1,ptr+0} ?

View 2 Replies View Related

C Sharp :: Storing Positions Of DrawRectangle Into A List - Only Stores Last X And Y?

Oct 12, 2012

I am trying to store values of X and Y into a list like this:

if (pictureBox1.Image != null) {
                    draw = true;
                    Graphics g = Graphics.FromImage(imageFile);
                    Pen pen1 = new Pen(color, 4);
                    g.DrawRectangle(pen1, e.X - 3, e.Y - 2, 5, 5);

[Code] ....

It stores all the nodes but it stores them as the same position, I am not sure how to make them act independently.

View 2 Replies View Related

C :: Calculate Final Grade By Adding 4 Numbers Minus Lowest Grade And Dividing By 3

Apr 7, 2013

I'm writing a program to calculate a final grade by adding 4 numbers minus the lowest grade and dividing by 3. My knowledge in c is not extensive I thought that a simple assigment operator would do the job but I'm getting a strange large numbers in the execution.

Code:
#include <stdio.h>
#include <stdlib.h>
main(){
int eg, g1, g2, g3, g4, fg, s1, s2, sg;

[Code] ....

View 4 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved