C :: Finding The Minimum Value Of Array
Sep 9, 2013
I am trying to make my program read a bunch of numbers in an array to find its maximum and minimum. the program will ask the user to enter in as much number as possible until they enter a non number/letter. i got my program to find the maximum value but the program couldn't read the minimum value. it always says zero. also everytime i enter the number 0, the program will just finish its loop statement. If i typed in a negative number, it'll read the negative number as its minimum.
Code:
#include <stdio.h>
int main()
{
//-------variables------------------
double list[1000]; // can hold 1000 items
int i;
char letter;
int max = list[0];
int min = list[0];
[Code] ....
View 5 Replies
ADVERTISEMENT
Mar 30, 2014
I am having a little trouble finding the minimum value in my two dimensional array. I feel like my code is right, but it could be returning the wrong value. Below is my code, this is just my "minimum" function!
Code:
int minimum( int array[][MAX], int size ) {
int i,j,min;
for( i = 0; i < size; i++ ) {
for( j = 0; j < size; i++ ) {
if( array[i][j] < min ) {
min = array[i][j];
[Code] .....
View 4 Replies
View Related
Oct 29, 2014
A function finds approximate maximum or minimum point of a second degree polynomial function (the point where the derivation will equal to zero ). The input polynomial function will be in the following format:
x2 + bx + c = 0 .
Your C function should take a , b and c as input parameters. Your C function also should take the srch_starting_point and stp_sze from the user. Finally, print the resulting maximum or minimum point (m_x, m_y) and step count (n_step) in your function.
For example, if the input is (a, b, c, srch_starting_point, stp_sze );
1 1 1 -3 1
Output similar to;
Maximum point results ( m_x, m_y, n_step )
-1 1 2
i can find the minimum point at first(Using derivation). After choosing starting point, staating point gets lower step size by step size. I can compare numbers to the minimum number. Afterwards, to find m_y i put m_x in the function. Finally, I put a counter to count steps.
View 4 Replies
View Related
Sep 17, 2014
I am trying to solve a MIP problem using C with Cplex linker. I need to find min value of two decision variables, as far as i know, i should write as a constraint because they are decision variables. "if" statement does not work for decision variables. here is some part of my model in C.
Code:
void TSPMIP(int Scenario, int Agency)
{
int i,j;
double tmpDouble;
[Code]....
View 7 Replies
View Related
Jan 22, 2013
I want to finding maximum and minimum values of 10 numbers using for loop and the program work wrong !!
#include <iostream>
using namespace std;
int main() {
int x;
int max = -999999999;
int min= 999999999;
[Code] ....
View 2 Replies
View Related
Feb 7, 2015
So I am trying to find the min, max, range, mean, median, and mode. The max number is right but the min isnt. Once that is corrected, i can get range and mean easily. Also, how to get mode and median. I am guessing the values in the arr2 would have to be sorted. This refers to the values in arr2 which are the calculated values in the formula. You can enter -2 and 2 for userMin and userMax.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(1);
float userMin;
[Code] .....
View 13 Replies
View Related
Jan 18, 2015
I'm trying to write a simple program that finds the minimum difference between two pairs of integers in a list/array. Of no surprise, it not work the first time I ran it so naturally i chucked in a whole bunch of print statements to debug
I encountered a peculiar error that is really stumping me.
#include <iostream>
using namespace std;
int closest_pair(int arr[]) {
int i=0;
int j=0;
int size=0;
int min=0;
[Code] .....
OUTPUT:
size_main: 20
arr_main: 80
arr[0]_main: 4
size: 2
arr: 8
arr[0]: 4
0: 34
1: -12
2: 18
3: 61
19: 75
closest_pair: 0
I'm printing the size of the array in main and inside the function...and inside the function its giving me a size = 2 which is incorrect though the size printed in main is correct (size = 20). I further narrowed down the discrepancy to this statement: sizeof(arr)
View 2 Replies
View Related
Jul 9, 2014
I want to search a 2D array to see if it contains a certain value x. I can come up with three different ways to do this. Provided I have:
const int Rows = 40;
const int Columns = 30;
int SampleData[Rows][Columns] = { ... }
Is there any real difference (in terms of performance etc.) between these, or are there an even better solution?
Solution 1:
for(unsigned row = 0; row < Rows; ++row) {
for(unsigned col = 0; col < Columns; ++col) {
if(SampleData[row][col] == x) {
// found
} } }
Solution 2:
int* data = &SampleData[0][0];
if(find(data, data + Rows * Columns, x) != data + Rows * Columns) {
// found
}
Solution 3:
int* data = &SampleData[0][0];
for(unsigned i = 0; i < Rows * Columns; ++i) {
if(*data++ == x) {
// found
} }
View 1 Replies
View Related
Dec 24, 2013
So for a project I'm working on, I'm using an array and generating it's values randomly but unique. Currently I'm working on a 3X3 array and the generated values are in the range from 1-9. So I wrote a function that will tell me the position of the cell whose value is 9. This is the function I wrote:
Code: void Llogaritje1(int t[3][3],int &i, int &j){
int y,l;
for(y=0;y<3;y++){
for(l=0;l<3;l++){
if(t[y][l]==9){
i=y;
j=l;
break;
}
}if(t[y][l]==9) break;
}
}
But it doesn't work on all cells. Seems like at cells t[1][0] and t[2][0] the values that i and j take are 0 0 since when I print them after excecuting the function that's what it returns. I really don't understand why.
View 4 Replies
View Related
Apr 11, 2014
Most of this program is working correctly, however when I get to the part on lines 70 to 78 to try and use the function on lines 103 to 113 to find out each diagonals value and then print each one back. But when this runs it only returns the value for [0][0] for each run through the loop and I'm not really sure why. I know its probably something simple but I'm just missing it,
Code:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<time.h>
4 #define MAX 100
5
6 void display_menu();
7 int check_option(int);
[Code]...
View 4 Replies
View Related
Mar 20, 2014
I've been trying for over an hour to think of the logic to find the mode of an array.I left out code for initializing the arrays, but all the values in the array are between 0-9. Check out my code below.
Code:
int mode( int array[], int size ) {
int i;
int count;
int max = 0;
int num = 0;
int mode;
[code]....
View 7 Replies
View Related
Apr 16, 2013
I am attempting to write a program that takes a number(picked by the user) of test scores in an array then calculates the average and outputs the number of passed and failed tests.
On a side note I am attempting to use array notation in the main function and pointer notation in the other functions
#include<iostream>
#include<iomanip>
using namespace std;
int numScores;
int counter = 0;
double *scores;
//Function Prototypes
[Code]...
View 3 Replies
View Related
Jan 14, 2013
i just want to check to see if one word (string) or one sentance is equal to any others within an array. My code
so basically i want to check if a is equal to either "sleep", "in", or "bed". Or even if the string within a is equal to "sleep in bed" as one string.
I have used the following but even if i get it correct and it says correct it says incorrect another 2 times because it is saying yeah you have gotten one right out of the possible 3. Even if I type "sleep in bed" it still does it (prints out incorrect another 2 times. Also are there any good books to start off with c++?
string a;
string aa[3] = {"sleep", "in", "bed"};
for(int i = 0; i < 3; i++) {
if(a == aa[i]) {
cout << "CORRECT!" << endl;
} else {
cout << "INCORRECT!" << endl;
}
}
View 18 Replies
View Related
Feb 17, 2014
"Write a program in C that finds the element of an array which all the previous are smaller and all the next are bigger than that.If there are more than one print the biggest. If there isn't any print "ERROR" .
EXAMPLE
If the array has 10 elements like this : {2,3,4,6,5,7,8,10,9,8}
The correct answer is 7 , because 2,3,4,6,5 are smaller than 7 and 8,10,9,8 are bigger than 7.
I am working on it for 2 weeks and I can't find a working solution />/>/>
There is the first try :
#include <stdio.h>
#include <stdbool.h>
int s_data[10]={2,3,4,6,5,7,8,10,9,8};
int main() {
int result,i,j,s_len ,tmp1,tmp;
bool GT_PREV,ST_NEXT;
[Code] .....
View 11 Replies
View Related
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
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
Feb 3, 2015
How i can find two equal int in array with O(n) time complexityand O(1) place complexity?
View 8 Replies
View Related
Mar 26, 2013
I know how to find a specific number in a multidimensional array, but I don't know how to have a specific number and find its coordinates.
View 2 Replies
View Related
Sep 9, 2013
I have to use numbers in my .txt file that are stored in an array. So i have to write a function to find the mode of the numbers in my array.
View 5 Replies
View Related
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
Mar 12, 2014
Above you said to find the middle value you should divide the array by two and then display the middle value (aka the median). For example, for an array consisting of 1 2 3 4 5, 3 would be the middle value.
My question is, how/what do I write to tell the program to display the middle value???
So far, I have done enough research and written enough code to receive values from the user and sort the array in ascending order. I simply don't know where to go from here (I started C++ about 1 month ago). I don't know how to tell it to pull the middle value.
I've researched "find_if", "nth_value" and a boat load of others with no luck
Here's what I've done so far:
#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
[Code]....
View 3 Replies
View Related
Mar 9, 2015
I keep getting an unresolved external symbol error in the double GetHighest function. Ive looked over the code many times and can figure out why its giving me this error.
//Question 1
// Copy source code below and save to a .cpp file (say lab11_01.cpp) and write statements to print what is indicated in the source code.
//In this question, you will implement code for finding the highest value in an array, and you do this in a function.
//This program gets a series of test scores and calculates and display the highest score.
#include <iostream>
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
void getTestScores(double[], int);
double getHighest(const double[], int);
[Code] ....
View 1 Replies
View Related
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
Aug 26, 2014
With out sorting and if the array size is changed and filled with different elements, mayb some that are all duplicates(in that case display message that says "No nonrepeated digits".
This seemed like a simple task but im failing to grasp some concept. if the array is 2,0,38,2,3,1,3 the first non repeated is 0.
nested loop is how i went. i feel like im going to far with the counters than need be. just need first non repeated. if n equals 2 an o equals 0. and it iterates comparing 0,38,2,3,1,3 to n which is 2. repCount will b 1. if repcount was 0 then n would be the first non repeated number. im trying to consolidate my if else statements so there are not so many. This program fails if the array gets bigger say 2,45,2,7,1,8,9,45,1,10 .....
for (n=0; n<sizeof(digits)/sizeof(int); n++) {
if(repCount<2) {
// nonRepCount=0;
for (o=n+1; o<sizeof(digits)/sizeof(int); o++)
[Code] .....
View 1 Replies
View Related
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
Oct 7, 2014
This is for a 2-d array, but if i understand how to do it for a 1-d array, I can do it for a 2-d. So to keep it simpler, I will just pretend its for a 1-d array. I am working on a project where i must enter the month number of a storm and then tell the user which month had the most storms. So I have an array and the user determines the size of it by entering the number of storms. Then, using a for loop, the user enters the month number for each storm and the data is used to fill the array. This is to be used in a part of a much larger piece of code. I made a sample code below to mimic what i want to do:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
/*Declare variables, i is a counting index used in for loop if necessary*/
[Code] ....
I believe this involves making some temporary array but where to start ....
View 2 Replies
View Related