Help

Hello people i need help for an exercise. Im from greece so sorry if the translation is bad.

A table of Cnxm real numbers is given. Write a C ++ program that:
1. Enter n, m and array C
2. Print array C
3. Calculate and print the largest element of array C as well as
its position within it, i.e. in which order and in which column it belongs.
4. Calculate and print the number of negative numbers in array C.
Application for n = 3, m = 4
C = 2.2 3.4 -5.6 10.3
7.1 8.0 -9.5 12.6
-1.2 4.3 -8.1 3.4

Last edited on
Δεν κάνουμε την εργασία σας για εσάς
What help do you need? What don't you understand? What attempt(s) have you made? Post your current code.
i'm new to c++ and i can't underastand the third and fourth question.
Ok. So what is your code for 1) and 2)? Post that and we'll take it from there.

What don't you understand about 3)? Do you know what is meant by 'largest' element?

If you have the array 1 2 5 6 4 3 then 6 is the largest element at position 3 (positions start at 0). Iterate the array and keep a position for the largest element found.

For 4), a negative number is < 0. Iterate the array and keep a tally of the number of negative numbers found.
Okay I will post it later. Thank you so much
#include <iostream>
#include <iomanip>

using namespace std;
int main(){

int array[3][4]= {{2.2,3.4,-5.6,10.3},{7.1,8,-9.5,12.6},{-1.2,4.3,-8.1,3.4}},n,m;
for(int row=0;row<3;row++){
for(int column=0;column<4;column++){
cout<<array[row][column]<<" ";

}
cout<<endl;
}
}


that is what i have done
you have 2.
1) asks for you to have the user put in nxm and the values, you have hard coded them.
some professors insist that students use a nonstandard feature of C++, dynamic arrays where the size is a variable (it must be a constant in standard c++). Its ok to do this for the program, but be aware that it is not standard/legal c++.

3) just like printing it, go through the entire array and figure out which one is largest. The safe thing to do is:
largest = c[0][0];
largerow = 0; largecol=0;
and then check each one ... for all the rows, for all the columns, if its bigger than current save the value and position. (you don't HAVE to keep the value, if you kept the position, but that is a little confusing for a beginner to worry about, do you see it?)

get 1 and 3 working and circle back to 4 if you still need help; you will learn a fair bit from 1 and 3 and may be ready for 4 by then.
Thank you very much for your help, but i can hardly do it. If it's possible can you write this code for me? I can't understan the c++ language and it's my last lesson for my degree. (p.s. the c++ programming is out of my future jobs)
no, I will not enable you to cheat.
we all went through school. I had to pass *racial theme omitted* literature, shrinkology, religious nonsense, art and music appreciation (translated: we had to go to senior level/grad student's art shows on campus so they had someone in attendance), and any number of other absolute crap classes to get my degree. Not only did they not help me get a job, they also had zero practical value (a high school level shop class would have been more useful than any of them).

I will tell you though that if you don't put in the time and do the work yourself, you will get more and more behind and fail it. But if you do it, you will learn something that will help you, even if its not your language of choice. How you solve problems and do things is universal, language is just syntax, though c++, admittedly, is harder syntax. If your degree does not involve code, you may be surprised later to find yourself having to program excel or a shell script or something anyway.
Last edited on
When I did my school 'A' levels (at 18, Computer Science, double maths, physics), the only non-subject class we had to attend was 'Use Of English' which basically taught you how to write essays. Not really relevant but still useful later for when I had to write reports. For my cs degree, every class was related to the degree course. With the lectures, tutorials and practical sessions, there was no time for anything non-related!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <vector>

int main() {
	size_t n {}, m {};

	std::cout << "Enter value for m (row}, n (col): ";
	std::cin >> m >> n;

	std::vector<std::vector<int>> array(m);

	for (auto& r : array)
		r.resize(n);

	std::cout << "Enter " << m * n << " values (by row): ";
	for (auto& r : array)
		for (auto& e : r)
			std::cin >> e;

	for (auto& r : array) {
		for (auto& e : r)
			std::cout << e << ' ';

		std::cout << '\n';
	}

	size_t mpr {}, mpc {}, noneg {};

	for (size_t r = 0; r < m; ++r)
		for (size_t c = 0; c < n; ++c) {
			if (array[r][c] > array[mpr][mpc])
				mpr = r, mpc = c;

			noneg += (array[r][c] < 0);
		}

	std::cout << "The maximum value is " << array[mpr][mpc] << " at row " << mpr + 1 << " col " << mpc + 1<< '\n';
	std::cout << "There are " << noneg << " negative numbers\n";
}

Last edited on
Topic archived. No new replies allowed.