C :: Finding The Closest Number To Another Number

Apr 10, 2014

Try not to make too much fun of me for my logic, but I'm having trouble with this. I am trying to make it so the program takes a 1 dimensional array and a 2 dimensional array, and checks to see what row in the 2 dimensional array is the closest to the 1D array.

To compute the value of the 1D array you take the first row first element in the 2D array, and the first element in the 1D array, subtract and the absolute value.

Example: | 4 - 3 | = 1;
1Darray = 1.

Full 1 Dimensional Array:
3 1 6 9

Full 2 Dimensional Array:
4 9 1 5
6 1 7 3
0 8 2 6

To compute the value of the first row in the 2D array,
| 4 - 3 | + | 9 - 1 | + | 1 - 6 | + | 5 - 9 | = 18.
| 6 - 3 | + | 1 - 1 | + | 7 - 6 | + | 3 - 9 | = 10.
| 0 - 3 | + | 8 - 1 | + | 2 - 6 | + | 6 - 9 | = 17.

Thus row 1 being the closest row to the 1 dimensional array.

The code I've written so far seems to be going down the right path. Have a look below...

Code:
int i,j,tempRow,tempTotal = 0,firEle,cloRow;
firEle = abs( x[0][0] - y[0] );

for( i = 0; i < size; i++ ) {
tempRow = 0;
for( j = 0; j < size; j++ ) {
tempRow += abs( x[i][j] - y[j] );

[Code] ....

The whole temp part is kind of confusing myself. What I'm thinking is that I can add all row values up using

Code: tempRow += abs( x[i][j] - y[j] ); , then I need to compare that value to see if it is close to the "firEle" which is value I need to get closest to.

View 5 Replies


ADVERTISEMENT

C :: How To Delete Number And Find Closest

Aug 3, 2014

Write a function which takes string of integer numbers and length of a string. Function need to return value of element which is close to mean of all numbers and transform string without that element. If are more numbers in same "distance" of mean, function need to find first and throw that number-element.

In function without []..
with pointers.

I wrote this code below, programm find mean of elements and if mean is equal to any element of string.. the programm return that number. Idk how to delete that number and how to find closest.. for ex:

String=[4,7,10,3]
sum=24
mean=6

The progoramm need to delete 7 and new string look like String=[4,10,3]

insert Code:
#include <stdio.h>
int main()
{
int n,i;
printf("Elements of string ?

[Code]....

View 9 Replies View Related

C++ :: Finding Max Number

Nov 5, 2013

#include <iostream>
using namespace std;
int max(int num1, int num2, int num3);
int main () {
int num1, num2, num3, large;

[Code] ....

For some reason it keeps num3 as large.

View 5 Replies View Related

C++ :: Finding How Many Factors A Number Have?

Jul 13, 2013

What is the most efficient algorithm for finding how many factors a number has? I've just been doing brute force division up to (n - 1) / 2 thus far. How can this be optimized?

View 5 Replies View Related

C++ :: Finding Nth Number Of A Sequence?

Sep 16, 2013

I have to write a program to find the nth number of the Ulam numbers.

It's a bit complicated to explain what an Ulam number is but Wolfram explains it very well here: [URL]

I have to find the nth Ulam number but I don't know what I have to do to get that. My program gives me all the Ulam numbers from a range of 0 to n.

What I want the program to do is tell me that the 49th Ulam number is 243.

/*
C++ Program to find nth Ulam Number
*/
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
int main() {
int num = 0;
vector<int> v;

[code]....

View 4 Replies View Related

C++ :: Finding If A Number Is Prime Or Not

Oct 3, 2013

cin.ignore (10);
return 0;
}

Its not working

View 2 Replies View Related

C++ :: Finding If A Number Is Prime?

Oct 3, 2013

We need it to say if the number is prime or not we need the equation for the if statement or if we have something else wrong to let me know.

#include <iostream>
using namespace std;
int main () {
int number;
bool countr;
for (countr = 0; countr <= 2; countr ++)

[Code] ....

View 1 Replies View Related

C++ :: Finding Highest Number In Array

Aug 12, 2014

after staring at this for awhile, I can't figure out why it won't work. It prints out numbers from 0 to 100, but will print out an absurdly high number as the highest number, like 790 or 640. I can't see why.

Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int find_highest(int array[]);
int find_highest (int array[], int size) {
int highest_num;
for (int i = 1; i < size; ++i) {
if (array[i] > array[i-1]) {

[code]....

View 1 Replies View Related

C :: Finding The Biggest Number In Array?

Feb 2, 2014

I'm supposed to find the biggest number (largest value), in any given array A with n numbers. (no floats)

My thoughts here are, check if A[0] > A[1]. if yes, check if A[0] > A[2]. If no, check if A[1] > A[2] etc.

I'm not sure how I can do this in code. I'm thinking if A[0] > A[1] is true, then set A[0] = A[k]. Kind of set it aside, and use that for the next if test. But I'm not sure how to do it.

This is my code so far.

Code:
int main(){
int A[7] = {12, 6, 9, 33, 2, 25, 53};
int i, k;
k = 0;

[Code]....

I'm aware of the flaws here, but this is the best I can do so far. How can I get the if test to use A[k] next, as A[k] will always be the biggest value?

View 11 Replies View Related

C++ :: Finding Number Of Lines Between Two Specified Characters

Feb 3, 2014

I am looking for a way to correctly count the lines between two specified characters/strings in a file. Here's the part I need work on:

getline( file, lines );
do {
if(lines.find("character")
{
++counter;
}
} while( !lines.find("story") );

I want the code to search for the first occurence of the word "character," and start counting the lines from that line until it hit the first occurrence of the word "story."

Right now, I am only getting a counter value of 1.

View 2 Replies View Related

C++ :: Finding Smallest Number In Array?

Oct 17, 2014

I was assigned to create a program that has a 10 x 8 two dimensional array and fill it with random numbers in the range 50 to 70 inclusively.( Got That part down). The second part is to make function named findSmallest that will find and print, appropriately labeled, the smallest number in the array.

I cant seem to get it working. The code for finding the smallest give me some weird number

Here my Code:

//Assignment 18 Program 2
#include <iostream>
using namespace std;

[Code].....

View 2 Replies View Related

C/C++ :: Finding Prime Factors Of A Number

Mar 18, 2015

I am trying to find the prime factors of a number but when i compile and run my code after i enter a number it just closes it doesn't print ....

included library : [URL] ....

#include "std_lib_facilities.h";
void prime(int n) {
int result;
result = 0;
int i;
for (i = 2; i == n*n; ++i)

[Code] .....

View 14 Replies View Related

C/C++ :: Finding Third Largest Number Without Using Array

Oct 12, 2014

I want to find third largest character in ascii as alphanumeritic. The problem is i cannot use arrays. How can I find third largest number ? Have to I compare all chars with each other ?

void third_largest(){
char ch;
do{
scanf("%c",&ch);
}
while(ch!=' ');
}

The chars can be hold up to press space ...

View 1 Replies View Related

C/C++ :: Finding Largest Number In Loop

Feb 10, 2014

I need to calculate the expected time it takes to do an activity using a formula provided and after the loop is broken it needs to show the number of projects processed and the project with the longest expected time. I've got everything working except for finding the the project with the longest expected time. What I have here just keeps displaying the number of the last project processed regardless of if it was the largest or not, specifically lines 54-61 is what i can't seem to figure out.

#include <stdio.h>
int main( void ) {
unsigned int counter;
int projectnumber;
int optimistictime;
int realistictime;

[Code] .....

View 1 Replies View Related

C/C++ :: Finding Missing Number In Array

Nov 27, 2012

I'm trying to write a program where in you input 9 numbers from 1-10, then it determines the missing number. Here's my code. It has a lot of errors. I want to improve it by setting conditions like no repetition/ 0< number <10.

#include <iostream>
using namespace std;  
int main() {
    int d[]={1,2,3,4,5,6,1,7,9, 10};
    int i=0,j=0,c=0,missing=0;  
for (i = 0; i < 9; i++)

[Code] ....

I'm so new to C++. It always prints out the number 10 even though I typed it already. How to determine the missing one.

View 3 Replies View Related

C++ :: Finding All Prime Numbers Using Class Number

Feb 20, 2015

Write a program that prompts the user to enter a number larger than 2. The program should use the Number class to determine the prime numbers between 2 and the number entered, and display them in the console window.I've been stuck for a while now and just lost in implementing classes and contstructors.

#include <iostream>
using namespace std;
int main(int argc, char * argv[])
{
cout << "Enter a number larger than 2: " << endl;
int n;
cin >> n;

View 1 Replies View Related

C++ :: Finding If A Number Is Prime - Large Numbers

Sep 15, 2013

I have a very large number in word ~6million digits long

I wish to make a program to check if it is prime.

View 5 Replies View Related

C++ :: Finding Largest Prime Factor Of A Number?

Mar 10, 2013

I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:

int is_prime(int number), I am unable to exit the loop.

Here is the code:

#include<iostream>
using namespace std;
int is_prime(int number) //the problem is in this function {
int num = number;
int factor=0;
do{
num++;
for(int i=1;i<(num+1);i++){

[code].....

So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.

I think the problem lies in the function: int is_prime(int number).

View 5 Replies View Related

Visual C++ :: Finding Value In Data File Closest To Calculated Data?

Jan 11, 2014

I'm reading from a file the middle column of data (Volts) where my test file "AP.txt" is opened by my program. Here is a segment of the data:

time Volts dV
49552 -66.20183 -0.01556807
49553 -67.76025 -0.01556495
49554 -69.30704 -0.01533247
49555 -70.81799 -0.0148486
49556 -72.26774 -0.0141133
49557 -73.63226 -0.01315276
49558 -74.89141 -0.01201764

Since the calculation I performed gives me -71.77 Volts, I need to match this value to the time that this occurs closest to using my program, and output the time that this occurs at.

Here is my program so far:
int main()
{
std::ifstream inFile;
inFile.open("AP.txt");
ofstream results_file ("maxvaluewithinput.txt");
float TimeAtdVMax = 0;
float VoltsAtdVmax = 0;

[Code]...

If you're curious, this program isn't for homework. It's part of the independent learning on C++ I'm doing for a Master's Thesis; the program will eventually model the APD90 of a ventricular action potential.

View 10 Replies View Related

C :: Function Macro For Finding Square Root Of A Number

Jul 13, 2014

How to write a function macro in C for finding the sqrt of a number?

View 2 Replies View Related

C++ :: Finding Mode - Number That Occurs Most Often In A Sequence Of Numbers

Dec 9, 2014

I was doing a side project from my textbook about finding the mode, which is the number that occurs most often in a sequence of numbers. I racked my brain on this for an embarrassing amount of time and finally came up with this, which I think works but for some reason I still feel like I'm not done.

#include "stdafx.h"
#include <iostream>
using namespace std;
void mode(int *, int);
void main() {
const int NUMBERS = 15;
int list[NUMBERS] = {99,73,56,14,28,42,93,64,51,26,56,16,38,81,98};
mode(list,NUMBERS);

[Code] ....

Console Output:

Final Mode 56
Press any key to continue . . .

There it is, I would have commented more but I couldnt think of the right words to explain everything.

View 4 Replies View Related

C++ :: Finding Smallest / Largest And Average Number - Do While Loop

Sep 10, 2013

I am stuck looking at whats the problem ....

#include <iostream>
using namespace std;
int main() {
float average, largest = 0, smallest = 0;
int count = 1;
int num;

[Code] .....

View 1 Replies View Related

C :: Finding Number Of Connected Components From Undirected Disconnected Graph

Feb 17, 2013

How to find the number of connected components from an undirected disconnected graph.

the input I'm getting is like this:

7
1 2
4 5
3 6
2 7

the top number is the number of vertices and the rest of the numbers are the edges, eg. 1--2 is an edge.

Is there a way that you can implement DFS algorithm to find the number of connected components? like increment a variable every time DFS is called or something?

View 1 Replies View Related

C++ :: Array To Template Class - Finding Largest Number From A Group Of Numbers

Sep 26, 2013

I'm trying to make an array that takes a group of numbers and finds the largest number into a template class.

template<class TYPE>
void Integers(TYPE data) {
int integers[] = {4, 25, 32, 85, 150, 12, 98, 200};
int i = 0;
int Max=integers[0];
for (i=1; i < 8; i++) {

[Code] ....

I'm sure I'm going about it all wrong, but I'm not sure as to get it so that it will accept the arrays input.

View 2 Replies View Related

C++ :: Finding String Of Number Present Inside Brackets After A Keyword In File

Apr 25, 2014

I am reading a file whick looks like following:

Code:
10 = NAND(1, 3)
11 = NAND(3, 6, 5)
15 = NAND(9, 6, 2, 8)

Problem: I have to find the word "NAND" and then find the numbers inside the brackets because they are the inputs to that NAND gate. I have written a code below but that code can detect the fixed number of inputs. I need a code which can detect any number of inputs (whether 2 inputs or more than two). But i don't understand how do i do that?

My code:

Code:
string input_str ("INPUT"), output_str ("OUTPUT"), nand_str("NAND");
while (getline( input_file, line ))
{
std::size_t guard_found = line.find(guard_str);

[Code].....

View 8 Replies View Related

C/C++ :: Finding All Prime Numbers To Nth Prime Number

Aug 24, 2014

I'm a new coder for C++, and I've recently learned java before starting this programming language. I'm attempting to find all prime numbers up to the number a user enters (i.e. User enters 10, System shows "1,2,3,5,7"),

#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
int num;
cout << "Enter a positive number." << endl;

[Code] ....

I've been looking at my forloop, and I can't seem to pinpoint the problem with it.

View 1 Replies View Related







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