I need to calculate the sum of negative elements of an array located between the largest and the smallest elements. Here's the code I've got so far:
#include <iostream>
using namespace std;
int main() {
int i = 0;
int arrSize = 0;
cout << "Enter an array size" << endl;
cin >> arrSize;
if (arrSize <= 0) {
cerr << "Entered number is negative or 0" << endl;
return 0;
} // check if arrSize is 0 or negative, print error if so
double* arr = new double [arrSize]; //declare array
for (i = 0; i < arrSize; i++) {
cout << "Enter array element " << i+1 << endl;
cin >> arr[i];
} // get elements of array
int max = arr[0];
for (int i = 1; i < arrSize; i++) {
if (max < arr[i])
max = arr[i];
} //find max value of array
int min = arr[0];
for (int i = 1; i < arrSize; i++) {
if (min > arr[i])
min = arr[i];
} //find min value of array
How would I go about finding the positions of max and min elements and getting the range in between them?
the Question was
Q.You are given an array of n elements. Each element depicts the height of a vertical bar.
For example, if you take the input array as [6,2,1,3] then the output should be.
what i though was
take value from user as a array
find the max value in that array
create a 2d boolean array with height as max value and width as array size of the user input array.
fill the boolean array vertically instead of horizontaly (methord how i did it is in the code below).
print the boolean array.
CODE:
#include <iostream>
using namespace std;
int main()
{
int n, i, j;
cout << "enter the size of array: ";
cin >> n;
int arr[n];
//INPUTING IN ARRAY!
for (i = 0; i < n; i++) {
cin >> arr[i];
}
//Calculating max-element
int max = arr[0];
for (i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
//creating a boolean array with height - max , width - n
bool new_arr[max][n];
//varables to input in the array vertically.
int k = 0, Q = 0;
for (i = 0; i < n; i++) {
for (j = max; j > 0; j--) {
Q = max - j; //to input in the array vertically, i.e at i =0 ,Q =0,1,2,3,4,5.
if (arr[i] >= j) {
new_arr[k][Q] = 1;
}
else {
new_arr[k][Q] = 0;
}
}
k++;
}
//printing the boolean array
cout << "FINAL ARRAY :" << endl;
for (i = 0; i < max; i++) {
for (j = 0; j < n; j++) {
cout << new_arr[i][j] << "\t";
}
cout << endl;
}
return 0;
}
I know there other far better methods but my mind is taken aback by this thought of doing
this question by this way only, I have tried another way and was successful but I am currently failing to do this question by this method(which my mind though of), please help.
The question I am trying to solve is the following:
Write a function that traverses (and prints) the element of an array with stride =7. To do this the update part in the loop will be i= (i+7) % n, where n is the array size.
Would this function visit all elements of the array? Try different array sizes to check when it is impossible to traverse all elements.
The code that I wrote below doesn't print the correct values in the arry even if the value of i is correct.
Can anyone help, I would really appreciate it.
#include <fstream>
#include <stdlib.h>
using namespace std;
int* CreateArray(int n);
void StrideArray(int arr[], int n);
int main()
{
int* arr = new int[3];
arr = CreateArray(3);
cout << "The Elements In The Array Are: " << endl;
for (int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
cout << endl;
StrideArray(arr, 3);
cout << "The Elements In The Array Stride 7 Are: " << endl;
for (int i = 0; i < 3; i++)
{
cout << arr[i] << " ";
}
delete[] arr;
return 0;
}
int* CreateArray(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (rand() % 100);
}
return arr;
}
void StrideArray(int arr[], int n)
{
int i = 0;
for (int j = 0; j < n; j++)
{
i = (i + 7) % n;
arr[j] = arr[i];
}
}
The problem is in StrideArray you read back the modified values of arr.
void StrideArray(int arr[], int n)
{
int i = 0;
int puffer=new int[n];
for (int j = 0; j < n; j++)
{
i = (i + 7) % n;
puffer[j] = arr[i];
}
for (int j = 0; j < n; j++){
puffer[j] = arr[j];
}
debete[] puffer;
}
Is a good way to write the function.
Also it visits all element only if n isn't dividable by 7. So if n is not 7,14,21,...
Also to use cout you have to #include <iostream>
Your StrideArray function needs fixing; you are iterating over j but using i to index, which remains constant; and you are reassigning value at one index to another where as you are supposed to print it:
void StrideArray(int arr[], int n)
{
int i = 0;
for (int j = 0; j < n; j=j+7)
{
cout << arr[j] << endl;
}
}
I modified the rest of your code for demo:
#include <iostream>
#include <stdlib.h>
using namespace std;
int* CreateArray(int n);
void StrideArray(int arr[], int n);
int main()
{
int* arr = new int[3];
arr = CreateArray(21);
cout << "The Elements In The Array Are: " << endl;
for (int i = 0; i < 21; i++)
{
cout << arr[i] << " ";
}
cout << endl;
StrideArray(arr, 21);
delete[] arr;
return 0;
}
int* CreateArray(int n)
{
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = (rand() % 100);
}
return arr;
}
its been 6 hours since I have been writing the code but to no avail, I don't no where I am making the mistake but I am making some. Its a frequency output program and output should be like this:
array[8] = {6,1,7,8,6,6,1,9}
Output:
6:3
1:2
7:1
8:1
9:1
But its repeating the same numbers in my code. Any help would be much appreciable.
int array[8] = {6,1,7,8,6,6,1,9};
int store[8];
int a =0;
int b =0;
int c=0;
int d = 0;
store[d] = array[b];
for (d = 0; d < 8; d++){
int count=0;
c = d;
b = d;
for (int e = 0; e < d; e++){
if (array[b] == store[e]){
store[d] = array[b];
b++;
e = 0;
}
else
{
store[d] = array[b];
break;
}
}
for ( int z = 0; z < 7; z++){
if (store[d] == array[z])
{
count++;
}
}
cout << store[d] << ":" << count << endl;
}
You may use a map first to store num->frequency and then a multimap to store freqeuncy => num.
Here is the working solution.
#include <map>
#include <algorithm>
#include <iostream>
int main()
{
int array[8] = {6,1,7,8,6,6,1,9};
// A map to store num => freq
std::map <int, int> freq;
// A map to store freq(can be duplicate) => num
std::multimap <int, int> freqCounts;
// Store num => frequency
for (int i = 0 ; i < 8; i++)
{
freq[array[i]] += 1;
}
// Now Store freq => num
for(auto const & iter : freq)
{
freqCounts.insert (std::pair<int,int>(iter.second, iter.first));
}
// Print in reverse order i.e. highest frequency first
for (std::multimap<int,int>::reverse_iterator rit=freqCounts.rbegin(); rit!=freqCounts.rend(); ++rit)
{
std::cout << rit->second << " : " << rit->first << '\n';
}
return 0;
}
You never seem to update the counters. Try this:
int array[8] = {6,1,7,8,6,6,1,9};
unsigned int store[10] = {}; // large enough to hold the largest array value,
// initialized to zero
for (int n : array) ++store[n]; // update counts
for (int i = 0; i != 10; ++i)
{
std::cout << "Frequency of int " << i << " is " << store[i] << "\n";
}
If the set of values that occur is sparse, or includes negatives, or simply does not fit into a dense range of integers nicely, you can replace unsigned int[10] with an associative container, e.g.:
std::map<int, unsigned int> store;
// algorithm as before
for (auto const & p : store)
{
std::cout << "Frequency of " << p.first << " is " << p.second << "\n";
}
I'm not sure what you are trying to do with the arrays. I have tried to follow the logic, but it's hard to see it with all the anonymous variable names. It seems like you are trying to look for duplicates earlier in the array, but the variable e never gets any other value than 0, so you will only be comparing with the first item in the array.
You can just look in the array itself for previous occurances, and once you know that the number is the first occurance, you only need to look for more occurances after it in the array:
int array[8] = {6,1,7,8,6,6,1,9};
for (int i = 0; i < 8; i++) {
// look to the left in the array if the number was used before
int found = 0;
for (int j = 0; j < i; j++) {
if (array[i] == array[j]) found++;
}
// go on if it's the first occurance
if (found == 0) {
// we know of one occurance
int count = 1;
// look to the right in the array for other occurances
for (int j = i + 1; j < 8; j++) {
if (array[i] == array[j]) count++;
}
cout << array[i] << ":" << count << endl;
}
}
I wanted to submit my solution which I think it´s a more easy one:
#include <iostream>
using namespace std;
int main() {
int n; //number of Elements in the vector
cin>>n;
int vec[n]; //vector with all elements
int v[n]; //vector with Elements without repetition
int c[n]; // vector which stores the frequency of each element
for(int i=0; i<n; i++)
cin>>vec[i];
int k=0; // number of Elements of the vector without Repetition, in the begining, it has 0 Elements
int j=0; //logic Counter, could be replaced with bool
for(int i=0; i<n; i++) {
for(int h=0; h<=k; h++) {
if(vec[i]==v[h]) {
c[h]++;
j=1;
break;
}
}
//if element i of the original vector is equal to element h of the second vector, then increment the frequency of this element
if(j==0) { //else if the element is not equal to any of the second vector, the Position of the 2nd vector is filled with the element, which in this case is the first of ist Kind.
v[k]=vec[i];
c[k]=1;
k++;
} //the number of Elements is increased by one to store another element;
else {
j=0;
}
}
cout<<endl<<endl;
for(int i=0; i<k; i++)
cout<<v[i]<<":"<<c[i]<<endl;
return 0;
}
#include<iostream>
#include<conio.h>
using namespace std;
main()
{ int count[10],key[10],n=10,m;
int i,j,k,temp;
cout<<"Enter The Size Of Array:-\n";
cin>>n;
int a[n];
cout<<"Enter The Elements in Array:-\n";
for(i=0; i<n; i++)
cin>>a[i];
for(i=0; i<n; i++)
for(j=0; j<n-1; j++)
{ if(a[j]>a[j+1])
{ temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
for(i=0,k=0; i<n; k++)
{ count[k]=0;
key[k]=a[i];
for(j=i; j<n; j++)
{ if(a[i]==a[j])
count[k]++;
}
i=i+count[k];
}
for(i=0; i<k; i++)
cout<<endl<<key[i]<<" Occurred "<<count[i]<<" Times\n";
getch();
}
/**
* The methods counts the frequency of each element in an array.
*
* Approach: The method checks if the element is already present in the <strong>Map of frequency</strong>.
* If it is not present, add it to the map with the frequency 1 else put it in the map with
* an increment by one of it's existing frequency.
*
* #param arr list of elements
* #return frequency of each elements
*/
public static Map<Integer, Integer> countFrequency(int[] arr) {
Map<Integer, Integer> frequency= new HashMap<Integer, Integer>();
for(int i = 0; i < arr.length; i++) {
if(frequency.get(arr[i])==null) {
frequency.put(arr[i], 1);
}
else {
frequency.put(arr[i],frequency.get(arr[i])+1);
}
}
System.out.println("\nMap: "+frequency);
return frequency;
}
I have to create a program that allows a user to fill in a (partial) Latin Square of order 4. You can use 0's to represent empty cells. The user will give the number to place, the row and column. The number should only be placed if it does not violate the properties of a partial Latin square and it shouldn't rewrite numbers that have already been placed.
I have an matrix that is outputting all zeroes now. So next I have to replace each of these values by what the user is inputting. The problem is I don't know how to do this.
Here is my code:
#include <iostream>
using namespace std;
const int ORDER = 4;
void fill (int m[], int order);
void outputMatrix (int m[], int order);
void replaceValue (int m[], int order, int n, int row, int column);
int main(){
int matrix[ORDER];
int row;
int column;
int n;
fill (matrix, ORDER);
outputMatrix (matrix, ORDER);
do {
cout << "Enter the number to place, the row and the column, each seperated by a space: ";
cin >> n;
cin >> row;
cin >> column;
}while (n > 0 || n <= ORDER);
if (n <= 0 || n >= ORDER){
cout << "Thank you";
cout << endl;
}
return 0;
}
void fill (int m[], int order){
for (int i = 0; i < order*order; i++){
m[i] = 0;
}
}
void outputMatrix (int m[], int order){
int c = 0;
for (int i = 0; i < order*order; i++){
c++;
cout << m[i] << ' ';
if (c == order){
cout << endl;
c = 0;
}
}
cout << endl;
}
void replaceValue (int m[], int order, int n, int row, int column){
for (int i = 0; i < order; i++){
m[order] = m[row][column];
m[row][column] = n;
}
}
How do I replace values in a Matrix in C++?
If you have a matrix, matrix[row][col] = value; would do the trick. However, I see that you allocate a single array. Make sure you look at this.
EDIT:
I looked closer at you code and you are doing some things wrong.
First:
matrix[ORDER]
will create a single array of ORDER values. If you want and ORDER by ORDER matrix try:
matrix[ORDER][ORDER]
Second:
You are calling:
void fill (int m[], int order){
for (int i = 0; i < order*order; i++){
m[i] = 0;
}
}
with an of size 4 and order == 4. This will loop outside the array and give you problems.
Try something like:
matrix[ORDER][ORDER];
for (int row = 0; row != ORDER; ++row)
{
for (int col = 0; col != ORDER; ++col)
{
matrix[row][col] = 0;
}
}
Hope this helps.
You can't really write arr[i][j] if arr is defined as arr[]. There's no information about the length of the row (how many columns there are).
You could use arrays of type arr[][4], and write your functions like so:
// The & is to pass by reference.
void print(int (&arr)[][4], int length)
{
for(int i = 0; i < length; i++) {
for(int j = 0; j < 4; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
But in my opinion for a low-order multidimensional array like this one, using a typedef for a vector of vectors is the better option:
typedef vector<vector<int> > Matrix;
void print(Matrix& arr)
{
for(int i = 0; i < arr.size(); i++) {
for(int j = 0; j < arr[i].size(); j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
In either case, writing arr[i][j] = k will behave as you expect.
The easiest way to clear/zero your matrix is that:
memset( &matrix, 0, sizeof(matrix));
;-)