Failing to properly print multidimensional array column? - c++

I recently have been having trouble with this simple problem, at the end of the code there are two blocks of text dedicated to finding the highest/lowest grade from what the user inputted.
That was simple enough and it works well, but I also need to print the a row corresponding to the lowest/highest graded student.
Essentially I'm working with the marks[5][4] array, the first array being the students, the second array being the exams.
If the lowest graded student was the student on array [2][] then I would I want to print out [2][i] using a for loop.
For the second array it works fine, the problem is I'm having problem on how to link the lowest graded student to the first array link.
I tried to use the count1 and count loops but they return a mess. I have been at this for a few hours and need a nudge in the right direction.
Thank you very much.
#include<iostream>
using namespace std;
int main()
{
char grade[5];
double avg[5];
string name[5];
int marks[5][4];
for (int i = 0; i < 5; i++) {
cout << "\n enter student" << i + 1 << "name :";
cin >> name[i];
cout << "\n enter four subject marks:";
for (int j = 0; j < 4; j++) {
cin >> marks[i][j];
}
}
for (int i = 0; i < 5; i++)
{
int sum = 0;
for (int j = 0; j < 4; j++) {
sum = sum + marks[i][j];
}
avg[i] = sum / 4;
if (avg[i] > 90.0 && avg[i] <= 100.0) {
grade[i] = 'A';
}
else if (avg[i] > 80.0 && avg[i] <= 90.0) {
grade[i] = 'B';
}
else if (avg[i] > 70.0 && avg[i] <= 80.0) {
grade[i] = 'C';
}
else if (avg[i] > 60.0 && avg[i] <= 70.0) {
grade[i] = 'D';
}
else if (avg[i] <= 60.0) {
grade[i] = 'F';
}
}
for (int i = 0; i < 5; i++)
{
cout << "\nName :" << name[i];
cout << "\tAverage : " << avg[i];
cout << "\tGrade : " << grade[i];
}
for (int i = 0; i < 4; i++)
{
int sum2 = 0;
for (int j = 0; j < 5; j++) {
sum2 = sum2 + marks[j][i];
}
cout << "\n";
cout << "The average of each exam " << i+1 << " is " << "%" << sum2 / 5;
}
//LOWEST
int count1;
int lowest;
lowest = avg[0];
for (count1 = 1; count1 < 5; count1++)
{
if (avg[count1] < lowest)
lowest = avg[count1];
}
cout << "\n";
cout << "The lowest average grade is is: " << lowest << ".\n";
for (int i = 0; i < 4; i++) {
cout << marks[count1][i] << " ";
}
cout << endl;
//Ends lowest.
//HIGHEST
int count;
int highest;
highest = avg[0];
for (count = 1; count < 5; count++)
{
if (avg[count] > highest)
highest = avg[count];
}
cout << "\n";
cout << "The highest average grade is is: " << highest << ".\n";
for (int i = 0; i < 4; i++) {
cout << marks[count][i] << " ";
}
cout << endl;
//Ends highest.
return 0;
}

From what I understand, you need to link the name with the lowest avg student
I recommend that instead of initializing lowest and highest with the average, initialize it with the index of the lowest and highest average. You can then compare the averages in the for loop like this
if (avg[lowest] >= avg[count1])
lowest = count1;
or you can follow Sam's solution, create another variable to store the index of the current lowest and highest avg

Related

How can I complete this code of question: find the kid with greatest number of candies

#include <iostream>
using namespace std;
int main()
{
int kids;
cout << "Enter the number of kids: " << endl;
cin >> kids;
int candies;
cout << "Enter the number of candies: " << endl;
cin >> candies;
int candies_arr[kids];
for (int i = 0; i <= kids - 1; i++)
{
cout << "Enter the candies you want to give to kid " << i + 1 << endl;
cin >> candies_arr[i];
}
int maxCandy = INT_MIN;
int minCandy = INT_MAX;
for (int i = 0; i <= kids - 1; i++)
{
if (candies_arr[i] > maxCandy)
{
maxCandy = candies_arr[i];
}
if (candies_arr[i] < minCandy)
{
minCandy = candies_arr[i];
}
}
cout << "The maximum candies were: " << maxCandy << endl;
cout << "The minimum candies were: " << minCandy << endl;
return 0;
}
How can I do changes in the arrays and do something so that I can find exactly the kid who has highest number of candies?
You can just add another variable to store the index of the greatest and smallest indexes.
int maxCandy = INT_MIN;
int maxIndex = 0;
int minCandy = INT_MAX;
int minIndex = 0;
for (int i = 0; i <= kids - 1 /*i < kids*/; i++)
{
if (candies_arr[i] > maxCandy)
{
maxCandy = candies_arr[i];
maxIndex = i;
}
if (candies_arr[i] < minCandy)
{
minCandy = candies_arr[i];
minIndex = i;
}
}
When printing number of kid with most candies.
cout << "Most candies has the kid " << i + 1 << endl;

Find the product of elements located between the maximum and minimum elements of an array

#include <iostream>
using namespace std;
int main()
{
int n, a, b,min,max, Prod = 1, Sum = 0;
cout << "Initialize an array n: ";
cin >> n;
do
{
cout << "Input the start value: ";
cin >> a;
cout << "Input the end value: ";
cin >> b;
if (!(a < b))
{
cout << "a is bigger than b, please enter new values " << endl;
continue;
}
} while (!(a < b));
int* lpi_arr;
lpi_arr = new int[n];
srand(time(NULL));
cout << "Int numbers from " << a << " to " << b << endl;
for (int i = 0; i < n; i++)
{
lpi_arr[i] = rand() % (b - a) + a;
cout << lpi_arr[i] << " ";
}
max = lpi_arr[0];
for (int i = 0; i < n; i++)
{
if (max < lpi_arr[i])
max = lpi_arr[i];
}
min = lpi_arr[0];
for (int i = 0; i < n; i++)
{
if (min > lpi_arr[i])
min = lpi_arr[i];
}
cout << "\nmin element is = " << min << endl;
cout << "\nmax element is = " << max << endl;
for (int i = max + 1; i < min; i++)
Prod *= lpi_arr[i];
for (int i = 0; lpi_arr[i] < 0 && i < n; i++)
Sum += lpi_arr[i];
cout << "Summ =" << Sum << endl << "Prod = " << Prod << endl;
delete[] lpi_arr;
}
The main purpose of this code is to calculate the sum of negative numbers of an array, and multiplication of elements located between the maximum and minimum elements of an array.
The problem is that the code implements only 1(one) as an answer, and I don't know how to change it. Every other part of the code works well, but if you have any recommendations I'd also like to read it. Waiting for your help.
Your issue is that you're confusing array indexes with array values.
Here you're assigning max (and same with min) to an array value.
max = lpi_arr[i];
Here you're treating max (and same with min) as an array index.
for (int i = max + 1; i < min; i++)
Prod *= lpi_arr[i];

Filtering values from an array and assigning them in different arrays C++

I am trying to filter an array given by the user on basis of whether it is positive even, positive odd, negative even, or negative odd.
And, based on this filtration, I am trying to put them in the respected array but my code is working for the 1st part; i.e. it is taking user array but the problem is: It is not entering my filtration code.
The code is here:
#include<iostream>
#include<limits>
#include <functional>
using namespace std;
int main()
{
int n ;
cout<<"\n Enter the size of array :-";
cin>>n;
int numbers[n];
int peven[n],podd[n],neven[n],nodd[n];
for (int i = 0; i < n ; i++){
cout<<"\n Enter value "<<i+1<<" = ";
cin>>numbers[i];
}
cout<<"\n\n";
for (int i = 0; i < n ; i++){
cout<<" "<<numbers[i];
}
cout<<"\n\n";
for(int j = 0;j<n;j++){
if ( ((numbers[j]%2) == 0) && (numbers[j] > 0) ) {
for(int i = 0; i<1; i--){
cin>>peven[i];
i++;
}
}
else if ( ((numbers[j]%2) == 0) && (numbers[j] < 0) ){
for(int i = 0; i<1; i--){
cin>>neven[i];
i++;
}
}
else if ( ((numbers[j]%2) != 0) && (numbers[j] > 0) ){
for(int i = 0; i<1; i--){
cin>>podd[i];
i++;
}
}
else {
for(int i = 0; i<1; i--){
cin>>nodd[i];
i++;
}
}
}
cout<<"\n The +ve even number array is :- "<<peven[n];
cout<<"\n The +ve odd number array is :- "<<podd[n];
cout<<"\n The -ve even number array is :- "<<neven[n];
cout<<"\n The -ve odd number array is :- "<<nodd[n];
return 0;
}
There are several problems in your code. Here is a list (which is not exhaustive) :
it is forbidden to create a constant size array with an integer which is not constant :
cin>>n;
int numbers[n];
As Sam Varshavchik mentioned, it is not possible to finish this loop :
for(int i = 0; i<1; i--)
You have no reason to read value from cin after you fill the array to filter. Line like this one should be modified :
cin>>peven[i];
Here is a correction :
#include <iostream>
#include <vector>
#include<limits>
#include <functional>
using namespace std;
int main()
{
vector<int> peven, podd, neven, nodd;
int n;
cout << "Enter the size of vector :-" << endl;
cin >> n;
vector<int> numbers(n, 0);
for (int i = 0; i < n; i++) {
cout << "Enter value " << i + 1 << endl;
cin >> numbers[i];
}
cout << endl;
cout << "Your vector contains : [";
for (int i = 0; i < n; i++) {
cout << " " << numbers[i];
}
cout << " ]" << endl;
cout << endl;
for (int j = 0; j < n; j++) {
if (((numbers[j] % 2) == 0) && (numbers[j] >= 0)) {
peven.push_back(numbers[j]);
}
else if (((numbers[j] % 2) == 0) && (numbers[j] < 0)) {
neven.push_back(numbers[j]);
}
else if (((numbers[j] % 2) != 0) && (numbers[j] >= 0)) {
podd.push_back(numbers[j]);
}
else {
nodd.push_back(numbers[j]);
}
}
cout << "\n The +ve even number array is : [";
for (int j = 0; j < (int)peven.size(); j++) {
cout << " " << peven[j];
}
cout << "] " << endl;
cout << "\n The +ve odd number array is : [";
for (int j = 0; j < (int)podd.size(); j++) {
cout << " " << podd[j];
}
cout << "] " << endl;
cout << "\n The -ve even number array is : [";
for (int j = 0; j < (int)neven.size(); j++) {
cout << " " << neven[j];
}
cout << "] " << endl;
cout << "\n The -ve odd number array is : [";
for (int j = 0; j < (int)nodd.size(); j++) {
cout << " " << nodd[j];
}
cout << "] " << endl;
return 0;
}

Solving Knapsack using recursive algorithm

So, I am trying to implement this algorithm from our textbook.
I wrote this :
// Knapsack_memoryfunc.cpp : Defines the entry point for the console application.
//Solving Knapsack problem using dynamic programmig and Memory function
#include "stdafx.h"
#include "iostream"
#include "iomanip"
using namespace std;
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
// ---CONCERNED FUNCTION-----
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = fmax(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
return table[i][j];
}
// --------------------------
void items_picked(int n, int max_wt)
{
cout << "\n Items picked : " << endl;
while (n > 0)
{
if (table[n][max_wt] == table[n - 1][max_wt]) // if value doesnot change in table column-wise, item isn't selected
n--; // n-- goes to next item
else // if it changes, it is selected
{
cout << " Item " << n << endl;
max_wt -= wt[n]; // removing weight from total available (max_wt)
n--; // next item
}
}
}
int main()
{
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
cout << " Optimum value : " << MNSack(n, max_wt);
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
items_picked(n, max_wt);
return 0;
}
Here is the question and output :
It seems like its correct on some places like optimum value, yet isn't fully acceptable.
I've tried to debug it, but its quite hard with recursive functions. Can someone please help?
int MNSack(int i, int j)
{
value = 0;
if (table[i][j] < 0)
{
if (j < wt[i])
value = MNSack(i - 1, j);
else
value = max(MNSack(i - 1, j), val[i] + MNSack(i - 1, j - wt[i]));
table[i][j] = value;
}
return table[i][j];
}
The problem comes in here. When your table item is greater or equal to 0, you will skip the recursion but still set the table item to 0, which won't be right if your table item is greater than 0.
You only need to update the table item when it needs to be change, so put it in the braces will correct this.
The bottom up solution.
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
int main()
{
int table[20][20] = { 0 };
int value, n, wt[20], val[20], max_wt;
cout << " Enter the number of items : ";
cin >> n;
cout << " Enter the Maximum weight : ";
cin >> max_wt;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << " Enter weight and value of item " << i << " : ";
cin >> wt[i] >> val[i];
}
// Initialization
for (int i = 0; i <= n; i++)
for (int j = 0; j <= max_wt; j++)
table[i][j] = 0;
// In practice, this can be skipped in a bottom up solution
for (int i = 1; i <= n; i++)
for (int j = 1; j <= max_wt; j++)
table[i][j] = -1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= max_wt; j++)
{
if (j < wt[i])
table[i][j] = table[i - 1][j];
else
table[i][j] = max(table[i - 1][j], val[i] + table[i - 1][j - wt[i]]);
}
}
cout << " Optimum value : " << table[n][max_wt] << endl;
cout << " \n Table : \n";
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= max_wt; j++)
if (table[i][j] == -1)
cout << setw(5) << "-";
else
cout << setw(5) << table[i][j];
cout << endl;
}
return 0;
}
You can see that this changes the recursion to a loop, and therefore avoids the global variables. It also makes the code simpler, so that you can avoid checking if the table item is valid (equal to -1 in your example).
The drawback of this solution is, it always traverses all the possible nodes. But it gains better coefficient per item because the recursion and double checking the table item costs more. Both top-down and bottom-up have the same order of complexity O(n^2), and it's hard to tell which one is faster.

Decimal point not displaying

I intend to let user key in positive integer value only but I realized that the output is wrong. For example largest value I entered is 100.6, the largest value displayed is only 100. I believe the problems come from atoi. Can anyone look at my code and tell me whats wrong?
#include<iostream>
#include<string>
using namespace std;
int main()
{
const int SIZE = 12;
string month[SIZE] = { "Jan", "Feb", "Mar","April","May","June","July","August","Sept","October","Nov","Dec"};
string highestMonth, lowestMonth;
char temp[SIZE];
double rainFall[SIZE], total = 0.0, average, highest, lowest;
bool flag;
int i = 0;
do{
flag = false;
cout << "Enter rainfall for " << month[i] << " : ";
cin >> temp;
for (int j = 0; j < strlen(temp); j++)
{
if (!isdigit(temp[j]))
{
if (temp[j] == '.')
continue;
cout << "Enter positive integer value only" << endl;
flag = true;
break;
}
}
if (flag == false)
{
rainFall[i] = atoi(temp);
total += rainFall[i];
i++;
}
} while (i < SIZE);
average = total / 12.0;
lowest = rainFall[0];
lowestMonth = rainFall[0];
for (int i = 1; i < SIZE; i++)
{
if (rainFall[i] < lowest)
lowest = rainFall[i];
lowestMonth = rainFall[i];
}
highest = rainFall[0];
highestMonth = rainFall[0];
for (int i = 1; i < SIZE; i++)
{
if (rainFall[i]>highest)
highest = rainFall[i];
highestMonth = rainFall[i];
}
cout << "Total rainfall:" << total << endl;
cout << "Average:" << average << endl;
cout << "Highest:" << highest << " in " << highestMonth << endl;
cout << "Lowest:" << lowest << " in " << lowestMonth << endl;
system("pause");
return 0;
}
atoi convert string representation of a number into an integer so "100.6" is converted into 100.
You can use atof
You set flag to true if the input has a decimal point and you have no code to process the decimal input whatsoever.
if (flag == false)
{
rainFall[i] = atoi(temp);
total += rainFall[i];
i++;
}
This code processes the input if flag is false, but there's no analogous code to handle decimals if flag is true.