need help with finals

hi guys, i just need help to pass the test. Need to learn some basics pretty fast. I am struggling with this task from a previous test i am looking at. i am doing something horribly wrong with j++; and ++k; i am not getting the answers that are in the solution. Please help me further :)

[code]
#include <iostream> using namespace std;
char txt[] = "ENDELIG-BLE-DET-EDINBURGH-I-VAAR-PIEMONTE-NESTE-I-HOST";
int main() { int i = 5; int j = i * i, k = 19 % 12 + i * 8; int n = j, m = k; while (txt[j] == txt[k]) { j++; ++k; }
cout << txt[j] << ' ' << txt[k] << '\n';
j = n; k = m;

while (txt[j++] == txt[k++]) ; cout << txt[j++] << ' ' << txt[k++] << '\n'; cout << txt[j] << ' ' << txt[k] << '\n';
i = 5 * 6 / 10; j = n / i; while (txt[i] != txt[j]) cout << txt[i++] << ' ' << txt[j++] << '\n'; cout << txt[i + n] << ' ' << txt[++j + n % 7] << '\n'; return 0;
> i am not getting the answers that are in the solution
And what are the 'correct' answers?


Nice try on the code tags.
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
#include <iostream>
using namespace std;
char txt[] = "ENDELIG-BLE-DET-EDINBURGH-I-VAAR-PIEMONTE-NESTE-I-HOST";
int main()
{
  int i = 5;
  int j = i * i, k = 19 % 12 + i * 8;
  int n = j, m = k;
  while (txt[j] == txt[k]) {
    j++;
    ++k;
  }
  cout << txt[j] << ' ' << txt[k] << '\n';
  j = n;
  k = m;

  while (txt[j++] == txt[k++]);
  cout << txt[j++] << ' ' << txt[k++] << '\n';
  cout << txt[j] << ' ' << txt[k] << '\n';

  i = 5 * 6 / 10;
  j = n / i;
  while (txt[i] != txt[j])
    cout << txt[i++] << ' ' << txt[j++] << '\n';

  cout << txt[i + n] << ' ' << txt[++j + n % 7] << '\n';
  return 0;
}

$ g++ foo.cpp
$ ./a.out 
V H
A O
A S
E B
A T


> i just need help to pass the test.
TBH, if what you posted is a measure of the standard of teaching, you're better off failing and finding some one/thing better to learn from.
Hey kimshady, can you please tell us in detail what exactly you're trying to do? Because I'm not able to understand what you're trying to do just from looking at your code.

Secondly you forgot your closing [/close] tag which can annoy a lot of people

And lastly please indent properly, something like so:
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
#include <iostream> 
using namespace std;

char txt[] = "ENDELIG-BLE-DET-EDINBURGH-I-VAAR-PIEMONTE-NESTE-I-HOST";
int main() {

	int i = 5, j, k, n, m;
	j = i * i;
	k = 19 % 12 + i * 8;
	n = j;
	m = k;

	while (txt[j] == txt[k]) {
		j++; ++k;
	}

	cout << txt[j] << ' ' << txt[k] << '\n';
	j = n; k = m;

	while (txt[j++] == txt[k++]);

	cout << txt[j++] << ' ' << txt[k++] << '\n'
		<< txt[j] << ' ' << txt[k] << '\n';

	i = 5 * 6 / 10;
	j = n / i;

	while (txt[i] != txt[j]) {
		cout << txt[i++] << ' ' << txt[j++] << '\n'; cout 
			 << txt[i + n] << ' ' << txt[++j + n % 7] << '\n';
	}

	return 0;
}


Because it is IMPOSSIBLE to read your code from your post, one would have to copy paste and indent your code themselves which most people wouldn't bother doing.
Last edited on
the answers are supposed to be :

A) V H
A O
A S
E B
A T
Sorry i did not post it right, my bad. I am doing something wrong with j++ and ++k. Because i am not getting the right index of the letters.
So, k=19 % 12 + i * 8 = 47, and j= 5*5= 25, and if i understand it correctly it is supposed to cout the index of the letters when they are : while (txt[j] == txt[k]).

Yes, programming is not my education, but i need to pass the course to move along. It is just a small subject. I am more interested in the main subjects regarding electrical engineering. Like three phase generators and motors, high voltage supplies and such.

Can you please post the original question? Because I'm overwhelmed. Well, I am mostly a dummy, but I think the original question would at least help other people who will try to help you.

Don't consider programming as an "education" it's like a regular speaking language, the more you use it the more you're good with it and so it might be hard at first. You should enjoy it while you're stuck with it. ^_^

Hva blir utskriften fra følgende program (litt hjelp: det blir 5 linjer):
this is the norwegian language, translated it would be something like this:

What would the following program printout? ( a little help: it will be 5 lines)


Well, you are right of course. The more you write in this language the more you understand. But, I have 100% job, educated electrician, good with electrical subjects, wife and kid. Not used to the programming, but good in those other subjects. I am doing this engineering degree after work and after my kid have went to sleep. There are simply too few hours in the day to become good at this:(

With that question what code did they give? The entire question was what?

Yes kimshady I can see what you're saying. You're right in that you shouldn't bother spending time. Just focus on what you're asked to do or rather what you need to do and that itself will help you learn what you need to know. And of course thanks to the internet, you're free to ask any stupid questions and there's still quite a lot of people who want to help! Get any small doubts clarified that will really help you.
> the answers are supposed to be :
Odd, that's the answer I got as well.

So what answer are you getting?

It's called software for a reason, you can change it as much as you like.
Feel free to edit further to add anything you like which may add clarity.
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
#include <iostream>
using namespace std;
char txt[] = "ENDELIG-BL"   // 0 to 9
             "E-DET-EDIN"   // 10 to 19
             "BURGH-I-VA"   // 20 to 29
             "AR-PIEMONT"   // 30 to 39
             "E-NESTE-I-"   // 40 to 49
             "HOST";        // 50 to 53
int main()
{
  int i = 5;
  int j = i * i, k = 19 % 12 + i * 8;
  int n = j, m = k;
  while (txt[j] == txt[k]) {
    j++;
    ++k;
    cout << "While1: j=" << j << ", k=" << k << endl;
  }
  cout << txt[j] << ' ' << txt[k] << '\n';
  j = n;
  k = m;

  while (txt[j++] == txt[k++]);
  cout << txt[j++] << ' ' << txt[k++] << '\n';
  cout << txt[j] << ' ' << txt[k] << '\n';

  i = 5 * 6 / 10;
  j = n / i;
  while (txt[i] != txt[j])
    cout << txt[i++] << ' ' << txt[j++] << '\n';

  cout << txt[i + n] << ' ' << txt[++j + n % 7] << '\n';
  return 0;
}


> I am more interested in the main subjects regarding electrical engineering.
> Like three phase generators and motors, high voltage supplies and such.
Software pervades all human endeavour. You'll be using s/w to design, model, test, simulate pretty much everything you do before committing it to physicality.



TBH, the code you posted is more fitting for an IOCCC entry than something you should be assessed against.
https://www.ioccc.org/

Grime: the entire question is to the code (i posted badly) : what would the program printout?(5 lines).

But, my question is, i get :

i=5
j= i*i

so j = 5*5 = 25
and
k = 19%12 + i * 8
k= 7 + 40 = 47

then i must be doing something wrong with j++ and ++k in the next step. Because i am not getting the letters V H.
What a horrible assignment. It requires you to unravel the block structure and find the index value into a long string. Yuck. Anyway, here is the program with lots of comments.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
//            0         1         2         3         4         5
//            012345678901234567890123456789012345678901234567890123
char txt[] = "ENDELIG-BLE-DET-EDINBURGH-I-VAAR-PIEMONTE-NESTE-I-HOST";
int
main()
{
    int i = 5;
    int j = i * i, k = 19 % 12 + i * 8; // j=25. k=47
    int n = j, m = k;			// n=25. m=47
    // Starting at 25 & 47, txt matches through "-I-"
    // So the loop executes 3 times.
    while (txt[j] == txt[k]) {
	j++;
	++k;
    }
    // Now j=28, k=50
    
    cout << txt[j] << ' ' << txt[k] << '\n'; // prints "V H"
    j = n;		     // j=25
    k = m;		     // k=47

    // Note that this is an empty loop ( selicolon at end of line)
    // As before, the loop executes 3 times, but THE CONDITION IS
    // EVALUATED 4 TIMES.  The 4th time is when it fails.  So
    // when the loop ends, j=29 and k=51
    while (txt[j++] == txt[k++]) ;

    // The ++'s below are post increment, so it outputs
    // txt[j] and txt[k], then increments j and k.
    cout << txt[j++] << ' ' << txt[k++] << '\n'; // print "A O"
    // Now j=30 and k=52
    
    cout << txt[j] << ' ' << txt[k] << '\n'; // prints "A S"

    i = 5 * 6 / 10;		// i = (5*6)/10 = 3;
    j = n / i;			// j = 25/3 = 8

    // Starting at 3 and 8, txt is "EL" and "BL"
    // The loop executes once, so i and j are incremented
    // once. Again, this uses post increment, so it prints
    // txt[3] and txt[8] then increments i and j
    while (txt[i] != txt[j])
	cout << txt[i++] << ' ' << txt[j++] << '\n'; // prints "E B"

    // i=4, j=9, n=25
    // i+n=29
    // Note that ++j is PREincrement, so
    // ++j + n % 7 is 10 + (25 % 7) = 14
    // So this prints txt[29] and txt[14]
    cout << txt[i + n] << ' ' << txt[++j + n % 7] << '\n'; // prints "A T"
    return 0;
}
thank you so much, it made all sense now. You really made my day.!!
At what point did your "solution" diverge?
Topic archived. No new replies allowed.