Im supposed to display grades in a graph that shows how many grades are in each interval 0-10, 10-20, 20-30 etc. By putting a * for each grade in the interval. My only problem is that when the array grade[] has 1, 2, 3 or 5 slots it puts an extra * in the 0-10 interval. It works with every other ammount of slots.
void sort_grades(int grades[],int students)
cout << endl << endl;
cout << " THE GRADES GRAPH"<< endl;
cout << "=============================="<<endl;
const int max_grade = 100;
const int interval=10;
for (int j = 0; j < max_grade; j+=10)
{
cout << j << " - " << (j+interval) << " : ";
for (int k = 0; k <= students; k++)
{
if (j==90 && grades[k] ==(j+interval) )
{
cout<< "*";
}
else if (grades[k] < (j+interval) && grades[k] >= j)
{
cout<< "*";
}
}
cout << endl;
}
I assume that students is the size of the array grades[].
In this case, you access too many elements in the following loop:
for (int k = 0; k <= students; k++)
Instead the line should read:
for (int k = 0; k < students; k++)
This problem does not only occur for the cases where grade[] has a size of 1, 2, 3 or 5. You only see the problem for that sizes, because the output depends on the content of the array.
Related
enter image description here
Hello everyone, I am trying to print a reverse heart shape in c++. While user will input a integer, and the program will display the reverse heart according to the value.
in the photo, the input value are 1,3,4
Now my code can only show the triangle but still missing the two small triangle, so hoping can get some guide here.
int size,space;
cout << "Input size: ";
cin >> size;
int rows = (size * 2)+1;
for (int i = 1, k = 0; i <= rows; ++i, k = 0)
{
for (space = 1; space <= rows - i; ++space)
{
cout << " ";
}
while (k != 2 * i - 1)
{
cout << "* ";
++k;
}
cout << endl;
}
{
for (int i = size; i >= 1; --i)
{
for (int space = 0; space < size - i; ++space)
cout << " ";
for (int j = i; j <= 2 * i - 1; ++j)
cout << "* ";
for (int j = 0; j < i - 1; ++j)
cout << "* ";
cout << endl;
}
}
I try to print a Inverted first, but I fail to print two at the same time.
I have created a 2d array, so lets say as I did in the code below the array elements shows like this:
3 4
8 2
I want to compare the" ROWS" and get the minimum number, like what I do with 3 & 4, I get 3 is the min number and with the second row between 8 & 2, I get 2 is the min number.
My problem is that I can't get it to compare the columns as in 3 & 8 to get the max out between them, and 4 & 2.
In the code it just outputs the max number in the rows it still comparing the "ROWS"
P.S sorry for the long explanation. I just had to clarify it .
int main()
{
int d;
int max;
//array dimention
cout << "Enter the array dimention :- " << endl;
cin >> d;
//max_min for the final maximum numbers
int max_num[d];
//the 2d array
int arr[d][d];
// array input
cout << "please enter the array elements :- " << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cin >> arr[i][j];
}
}
//array output
cout << "the array elements you enterd :-" << endl;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
//the max numbers
for (int i = 0; i < d; i++) {
max = arr[0][i];
for (int j = 0; j < d; j++) {
if (max < arr[j][i]) {
max = arr[j][i];
}
}
max_num[i] = max;
}
cout << "the maximum elements in array :-" << endl;
for (int i = 0; i < d; i++) {
cout << max_num[i] << endl;
}
}
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 3; j++)
{
cout << "Enter meal " << j + 1 << " for day " << i + 1 << ":" << endl;
cin >> menu[i][j];
switch (menu[j][k])
{
//cases here
}
sum += calories;
}
}
cout<<calories;
This is a portion my code, i need to store 3 meals per day for 1 week which i managed to do, but the problem is the calculation part.. i need to calculate each days calories intake and display it but my codes are calculating the total calories intake for 1 week.. i have no idea on how to calculate it for each day
Can you guys guide me?
Why not print it each day (every iterate of the outer loop) and set the sum to 0 each day (every iterate of the outer loop).
for (int j = 0; j < 7; j++)
{
sum = 0;
for (int k = 0; k < 3; k++)
{
cout << "Enter meal " << k + 1 << " for day " << j + 1 << ":" << endl;
cin >> menu[j][k];
switch (menu[j][k])
{
// cases here.
}
sum += calories;
}
std::cout << sum << std::endl;
}
I am trying to write a program to count each number the program has encountered. by putting M as an input for the number of the array elements and Max is for the maximum amount of number like you shouldn't exceed this number when writing an input in the M[i]. for some reason the program works just fine when I enter a small input like
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Answer:
5 2 3
But when I put a big input like 364 for array elements and 15 for example for max. the output doesn't work as expected and I can't find a reason for that!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
You have general algorithm problem and several code issues which make code hardly maintainable, non-readable and confusing. That's why you don't understand why it is not working.
Let's review it step by step.
The actual reason of incorrect output is that you only iterate through the first Max items of array when you need to iterate through the first Max integers. For example, let we have the input:
7 3
1 1 1 1 1 2 3
While the correct answer is: 5 1 1, your program will output 5 5 5, because in output loop it will iterate through the first three items and make output for them:
for (int i = 0; i < Max; i++)
for (int j = 0; j < ArrayValue; j++)
if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1
It will output answers for first three items of initial array. In your example, it worked fine because the first three items were 1 2 3.
In order to make it work, you need to change your condition to
if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
cntr += 1;
}
It will work, but both your code and algorithm are absolutely incorrect...
Not that proper solution
You have an unnecessary variable element_cntr - loop variable i will provide the same values. You are duplicating it's value.
Also, in your output loop you create a variable n while you have a loop variable i which does the same. You can safely remove variable n and replace if (M[n] == checker[j]) to if (M[i] == checker[j]).
Moreover, your checker array is a full copy if variable M. Why do you like to duplicate all the values? :)
Your code should look, at least, like this:
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int cntr = 0;
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue >> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
if (M[i] > Max)
{
cout << "the element number " << i << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (i == M[j])
{
cntr ++;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
}
return 0;
}
Proper solution
Why do you need a nested loop at all? You take O(n*m) operations to count the occurences of items. It can be easily counted with O(n) operations.
Just count them while reading:
using namespace std;
int arraySize;
int maxValue;
int counts[1000];
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> arraySize >> maxValue;
int lastReadValue;
for (int i = 0; i < arraySize; i++)
{
cin >> lastReadValue;
if (lastReadValue > maxValue)
cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
else
counts[lastReadValue]++; // read and increase the occurence count
}
for (int i = 0; i <= maxValue; i++)
{
if (counts[i] > 0)
cout << i << " occurences: " << counts[i] << endl; // output existent numbers
}
return 0;
}
const int N = 5;
int person[] = {2, 3, 12, 5, 19};
int big = 0;
int small = 100;
int i;
for (i = 0; i <= N; i++)
{
cout << "Person[" << i << "] ate: " << person[i] << endl;
//cin >> person[i];
for (int j = i+1; j <= N; j++)
{
if (person[i] < person[j])
{
int tmp = person[i];
person[i] = person[j];
person[j] = tmp;
}
}
if (person[i]>big)
big=person[i];
if (person[i]<small)
small=person[i];
}
cout << "Person[" << i << "] ate the most pancakes: " << big << endl;
cout << "Person[" << i << "] ate the least pancakes: " << small << endl;
cout << "Sorted:" << endl;
for (i = 0; i < N; i++)
{
cout << "Person[" << i << "]: " << person[i] << endl;
}
system("pause");
output
Where I messed up those arrays it keeps showing me 2 but the bubble sorting works. And the other question is how to get the array index from the smallest value and the array index from the highest value?
In C++ arrays are ZERO 0 indexed. So you should change your for loops like this:
for (i = 0; i < N; i++)
^^^
{
// some code...
for (int j = i+1; j < N; j++)
^^^
{
// some code...
Because:
index -> 0 1 2 3 4
person[] = {2, 3, 12, 5, 19};
In your code the value of i and j will increment up to N which is 5. That means you are trying to access the array named person's 5th index which will create array index out of bound error.
Array indexing starts from zero so if there are N elements, last element will be at N-1 index.Accessing element at index N is going out of bounds.
for (i = 0; i <= N; i++)//should run till `N-1`
^^^
for (int j = i+1; j <= N; j++)//should run till `N-1`
^^^
If you want index of the highest and the lowest element,since you have sorted the array,the smallest values's index would be 0 and highest value's index will be N-1(opposite in your case because you have sorted the array in decreasing order)