Using arrays to find a maximum [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I take in an array I go through all it's elements, if any member of the array is greater than j, than it makes that element the new j. For some reason I keep getting back that the maximum is 4. What's going on?
##include <iostream>
using namespace std;
int MAXIM(int arg[],int sz){
int j = 0;
for(int i = 0; i < sz; i++){
if(arg[i] > j){
j = i;
}
}
return j;
}
int main(){
int coolarr[5] = {5,17,45,7,34};
int maxxy = MAXIM(coolarr, 5);
cout << maxxy << endl;
}

j = i; this line is wrong; it should be j = arg[i];.

Related

C++, error: ā€˜iā€™ was not declared in this scope in a for loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to do a code that pop_back()-s numbers[i] into either one of these vectors: oddnumbers and evennumbers
but unfortunately I got ' 'i' was not declared in this scope'
the code is following:
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {2,4,3,6,1,9};
std::vector<int> evennumbers;
std::vector<int> oddnumbers;
for (i = 0; i < numbers[6]; i++) {
if (numbers[i]%2 == 0) {
evennumbers.pop_back(numbers[i]);
}
else if (numbers[i]%2 != 0) {
oddnumbers.pop_back(numbers[i]);
}
}
}
As the compiler says, there is no declaration of i.
Replace
for (i = 0; i < numbers[6]; i++) {
by
for (int i = 0; i < numbers[6]; i++) {

c++ program to raise a number to power gives error (expected unqualified id before 'for') [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
for loop isn't working in this cpp program
I tried changing for conditions but that doesn't work either.
#include <iostream>
using namespace std;
int num(int num1, int num2) {
int result = 1;
for (int i = 0; i < num2; i++) {
result = result * num2;
return result;
}
}
int main() {
cout << num(2, 3);
return 0;
}
please move return to out of loop .like:
for (int i = 0 ; i < num2 ; i++){
result = result * num2;
}
return result ;

Why is my program saying that 5 * 5 is equal to 104126025? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I created a function to return the result of all the odd numbers in a vector.
int oddProduct(std::vector<int> arr) {
int sum = 1;
for (int i = 0; i <= arr.size(); i++) {
if (arr[i] % 2 != 0){
sum *= arr[i];
std::cout << " " << arr[i];
}
}
return sum;
}
The function worked with every other vector I inputted and the print statements even show that the elements of the vector I multiply are both equal to 5.
(btw I was using repl.it)
i <= arr.size()
The legal values of the subscript are 0 through size-1 inclusive.
Use a range-based for statement to avoid the mistake (and the repeated dereferences)

What is the problem of c++ no value sort()? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
While I was studying algorithms, I was trying to run a sort order, but there was an error.
It says there are no members in the value, but there is an error even if you declare the structure. Do you know why?
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct table{
int startValue;
int finishValue;
};
bool cmp(table f, table s)
{
if (f.finishValue == s.finishValue)
return f.startValue < s.startValue;
else
return f.finishValue < s.finishValue;
}
int main(int argc, char *argv[])
{
int maxTimeTable;
cin >> maxTimeTable;
vector<table>t(maxTimeTable);
for (int i = 0; i < maxTimeTable; i++)
{
cin >> t[i].startValue >> t[i].finishValue;
}
sort(t.startValue(), t.finishValue(), cmp); //Error Occurrence Point
int cnt = 0;
int n = 0;
for (int i = 0; i < t.size(); i++)
{
if (n <= t[i].startValue)
{
n = t[i].finishValue;
cnt++;
}
}
}
You are treating t as a table-object
sort(t.startValue(), t.finishValue(), cmp); //Error Occurrence Point
but it's not, right? t is defined as vector<table>t(maxTimeTable);. And correctly, the compiler provides an error, since vector<table> does not have a method startValue nor a finishValue.
You probably meant to sort the vector by accessing the iterator to the first element, and to the last element:
sort(t.begin(), t.end(), cmp);

Generating a random out of 2 dice roll [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I fixed the typos that I had in my code, and now it runs perfect
int rollDice(int diceRoll[], int numberRolling) // Random dice rolls
{
int values = 0;
for (int i = 0; i < numberRolling; i++)
{
diceRoll[i] = 1 + rand() % 6;
}
for (int i = 0; i < numberRolling; i++)
{
values = values + diceRoll[i];
}
return values;
}
You have a typo in your first for
for (int i = 0; i < numberRolling, i++)
Should be
for (int i = 0; i < numberRolling; i++)