Programmers, help me out Created 18 years ago2006-05-13 11:39:46 UTC by PTS PTS

Created 18 years ago2006-05-13 11:39:46 UTC by PTS PTS

Posted 18 years ago2006-05-13 11:39:46 UTC Post #179760
I need the solution for two tasks, anyone with programming skills, please help!

1.
Write a program, which displays the first item of the line:
https://www.bfu.bg/materiali/pr/programirane_kz1_2006_files/image026.gif
for which the expression is true:
https://www.bfu.bg/materiali/pr/programirane_kz1_2006_files/image028.gif
'a' and 'eps' are keyboard input values.

2.
Given a quadratic (square?) matrix of strings with maximal lenght of 6, write a program, which checks whether a given string 's' can be found in the part above the secondary main diagonal of the matrix.

The programs have to be written in Borland C++. Any help/suggestions would be greatly appretiated.
Posted 18 years ago2006-05-13 11:52:48 UTC Post #179762
Host those images somewhere else. https has been blocked here.

I have some programming experience with Win32 apps, but i use Visual Basic.NET to make them, not C++.
Posted 18 years ago2006-05-13 11:54:08 UTC Post #179763
The first item is clearly 1; it says so. Do you mean the second, or what?
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-13 12:05:42 UTC Post #179765
Pfff. Do you really understand that? Bladiebla...
Posted 18 years ago2006-05-13 13:18:20 UTC Post #179786
Er, yes, actually, it's a geometric series.
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-13 15:29:44 UTC Post #179817
Yes, 7th is right.

Zie het als een wiskundige verglijkking TMA.
Posted 18 years ago2006-05-13 15:31:53 UTC Post #179819
It looks like a kids drawing. :P
Posted 18 years ago2006-05-13 18:11:23 UTC Post #179853
1 is pretty much a mathematical problem, one which I'm not going to solve. :P

2 is easier. A possible solution would be to use two loops, the outer loop going over all rows and the inner loop checking a decreasing amount of cells. Some pseudocode to get you started:

string matrix[width][height];
for(i = 0; i < height - 1; i++)
for(j = 0; j < width - i - 1; j++)
if(matrix[j][i] == "s")
return true;
Posted 18 years ago2006-05-13 18:20:46 UTC Post #179855
The second term of that series will be 0.5(1+a), if you want that.
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-14 06:13:27 UTC Post #179942
1.
Write a program, which displays the first item of the line:
User posted image
for which the expression is true:
User posted image
'a' and 'eps' are keyboard input values.
The first item is not going to be 1, because it has tobe the first one to comply with the expression, not just the first item.

What should I use for it, an array or something? Or maybe a function to generate each member and another one to check whether the expression is true and return the member ID and value... I don't know, maybe I should make a block scheme first (best results will be achieved with a genetic matrix, but I have little wish to make one for such a simple program).

For the second one I'd first liek to know what is a secondary main diagonal and then it'll be rather easy.

EDIT: My brother sent me this solution, but he said it wouldn't compile, I'm analyzing it now, no idea if it has mistakes yet:

[quote]#include<iostream.h>
#include<math.h>
int main()
{double x,z,eps,a;
cout<<"a=";
cin>>a;
cout<<"eps=";
cin>>eps;
z=1;
do{
z=1;
x=(z+a/z);
z=x;
if(abs(a/z+z-1)<eps);
}
while(1);
cout<<z;
return 0;}
while(1);[/quote]
Posted 18 years ago2006-05-14 06:43:15 UTC Post #179945
Hmmm, i really have no idea how to help you there, i dont use C++.
But you might want to make it indented so you can recognize the different objects.
Posted 18 years ago2006-05-14 07:22:13 UTC Post #179955
I could help ya with the programming part, if I understood those equasions.. :lol:
Daubster DaubsterVault Dweller
Posted 18 years ago2006-05-14 08:27:35 UTC Post #179969
I've made a couple of changes to the example code (which was totally messed) and I've came up with this:

[quote]#include <iostream.h>
#include <math.h>

int main() {
double x,x1,eps,a;
cout<<"Input value for 'a': ";
cin>>a;
cout<<endl<<"Input value for 'eps': ";
cin>>eps;
cout<<endl<<"Working";
int n=1;
x=1;
if ( (abs(a/x*x-1)<eps)==true ) {
	cout<<endl<<"Item ID: "<<n<<endl<<"Item value: "<<x<<endl;
	return 0;
}
do {
	n++;
	x1=x;
	if ( n%10000==0 ) { cout<<"."; }
	x=(x1+a/x1)/2;
} while ( (abs(a/x*x-1)<eps)==false );
cout<<endl<<"Item ID: "<<n<<endl<<"Item value: "<<x<<endl;
return 0;
}[/quote]

I think TWHL doesn't allow indenting :x I certainly hope Alex will allow it in TWHL3.

It works, but I don't know if it's correct, because it's taking alot to find the correct item... We'll see about that later.
Posted 18 years ago2006-05-14 09:49:00 UTC Post #179977
x1 = x? What? x1 = 1 :|.

Perhaps you wanted xn = x, or are you moving into the world of math.h here?
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-14 10:16:35 UTC Post #179980
desired_endpoint = 10000; //iterations you want
xn = 1;
n=1
do {
xnext = 0.5 * (xn + a/xn);
if( abs(a/(xnext*xnext)-1) < eps )
cout<<"for n="<<n<<": "<<xnext<<" is valid"<<endl;
else
{
cout<<"for n="<<n<<": "<<xnext<< " is invalid"<<endl; exit(1);
}
// the exit in the 'else' assumes you want to
// quit with an invalid result
n++;
xn = xnext;
} while n < desired_endpoint;
Posted 18 years ago2006-05-14 15:05:32 UTC Post #180023
Woah... That actually worked! It's pretty much like mine, only I tried some weird expressions like "(abs(a/x*x-1)<eps)==true" which totally qrong, I didn't needed the "true" thingie :P

Alex: 'x1' is a valid variable name ;)

Thanks, BJ, if we ever meet I'll buy you a bottle of vodka :D

Now I just need the solution for the second task.
Posted 18 years ago2006-05-14 15:28:48 UTC Post #180029
Now I just need the solution for the second task.
I've given you some pseudocode, it shouldn't be too hard from there. ;)
Posted 18 years ago2006-05-14 15:45:45 UTC Post #180035
Alex: 'x1' is a valid variable name
Yeah, but... for storing xn in?
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-14 22:23:09 UTC Post #180117
I'll buy you a bottle of vodka
Hoohaw! That's my kind of programming!

Can't help much with the second task as I'm not sure what a "quadratic matrix" is nor what a "secondary main" diagonal is.

"Secondary main" seems like an oxymoron like "sanitary sewage." :)
Posted 18 years ago2006-05-15 02:30:32 UTC Post #180146
:D

I think it's probably just the fact that the terms are rather different in Bulgarian.
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-15 09:30:59 UTC Post #180182
Ah.. you're probably right, SM. Wow, don't I look like the nicest guy on earth. PTS - I apologize if there was any offense in my statement.

Anyway, as I recall, "string" itself is not a variable type so one would be starting with a two-dimensional array of char arrays.

Although Captain P's outline describes the matrix, you have to use functions to compare strings.

char* matrix[width][height]; // an array of char pointers
char s[6]; // the string to find
/... load the matrix and s with null-ended char strings .../
for(i = 0; i < height - 1; i++)
for(j = 0; j < width - i - 1; j++)
if( strcmp(matrix[i][j],s)==0 ) cout<<"string found at "<<i<<", "<<j<<endl;

strcmp() returns 0 if the strings match. If case is not important then use stricmp().
Posted 18 years ago2006-05-15 10:51:33 UTC Post #180208
Main diagonal seem to be the one going all the way through the matrix (which is an array of chars, yah). The primary diagonal is the one going from top left to bottom right and the secondary is the other one. I'll try to stitch that code together.

PS: Any suggestions for input values for the first program that return diffirent than second item? I calcuated things on paper and all of the times the first or the second items where correct :/

EDIT:
Yeah, but... for storing xn in?
It's used to store the current item so we can use it to calculate the next one.
Posted 18 years ago2006-05-15 13:11:51 UTC Post #180252
Yeah. That's x_n, not x_1. x_1 is the very first item.
Seventh-Monkey Seventh-MonkeyPretty nifty
Posted 18 years ago2006-05-15 13:17:20 UTC Post #180255
@BJ: I assumed he meant "s" rather than a generic string, but even then, the == operator works just fine. Both for char* as for string. Unless you really need non-case-sensitive checks. :)
Posted 18 years ago2006-05-15 14:50:22 UTC Post #180270
@CP: with respect to 's', I understand. I just generalized it since he said "given string," assuming he may have to input it, etc.

With respect to string comparisons, I disagree. There isn't a "string" fundamental variable type in C++. In the case of char pointers, the == op will compare pointers, not strings. That's why the compare function(s) must be used. Matrix[x][y] and the variable s are character pointers (i.e., address variables), not the strings themselves.

E.g.:
char s[5], t[5];
strcpy(s,"abc");
strcpy(t,"abc");
if( s==t ) cout << "equal" << endl;
else cout << "not equal" << endl;
if( strcmp(s,t)==0 ) cout << "equal" << endl;
else cout << "not equal" << endl;

The output will be:
User posted image
:

because the pointers s and t are not equal but the strings they point to are equal.
Posted 18 years ago2006-05-15 17:24:34 UTC Post #180313
STL provides a string class. Not a 'fundamental' type perhaps, but a usefull one nonetheless. It's member function c_str() returns a char* just in case you need that for function passing and whatnot.

I usually don't work with char arrays, but char pointers instead. Though these seem to be similar, using char* rather than char[] gives different results. For example, the == operator does actually compare the strings they contain:

char* a = "hello";
char* b = "hello";
if(a == b)
cout << "True";
else
cout << "False";

This prints True to screen. Changing b to "hello1" gives False. Comparing a string (#include <string>) with a char* gives the same results.

As far as I understand, the == operator compares char pointers as if they were null-terminated strings, comparing char by char, while it compares char arrays by their adresses.
I tested casting char arrays to char pointers, too, which gave interesting results:

((char*)s == (char*)t) == false, while
((char*)s[0] == (char*)t[0]) == true, provided both are assigned the same string value.

Not everything is clear to me, but there's a subtle difference between arrays and pointers. &s, for example, is the adress of the array, and while it's the same adress as the first element in the array (&s[0]), it's not of the same type: (&s + 1 == &s[0] + 1) == false.
Posted 18 years ago2006-05-15 22:34:41 UTC Post #180337
I've used several template libraries and don't much care for them. They work pretty well when used strictly as designed but, as you demonstrate, they don't always provide consistent interpretations when referencing, dereferencing and casting are involved.

When you cast ( (char*)s, for instance) and reference (&s), I suspect the compiler reverts to standard C++ rules and your results are as expected. Casting is not an operator function (so is not overloaded) and, I'm pretty sure, referencing isn't usually considered to be overloadable, either.

(char*)s is the pointer stored at s, (char*)t is the pointer stored at t and they should be different.

(char*)s[0] would mean "interpret the first character of the array as a pointer" and, if the s[0] and t[0] are the same, the results would be the same.

&s would be the address of the variable s, not the addresss of the array. &s[0] would be the address of the array and &s[0]==s, not &s.

Guess that's why I stick to good ol' ANSI C++! :biggrin:
Posted 18 years ago2006-05-16 03:56:15 UTC Post #180360
I'll buy you a bottle of vodka
Hoohaw! That's my kind of programming!
Bah, if only I could program :[
RabidMonkey RabidMonkeymapmapmapfapmap
Posted 18 years ago2006-05-16 10:52:15 UTC Post #180391
Go for it, RM! A good book and the samples included in most software packages will give you a good start!
Posted 18 years ago2006-05-16 14:08:52 UTC Post #180406
but, as you demonstrate, they don't always provide consistent interpretations when referencing, dereferencing and casting are involved.
I wasn't demonstrating anything with the STL string, but I tried to explain that char arrays and char pointers are a little different (than each other). :)
Namely, char*'s are compared as if they were null-terminated strings, char[]'s are compared by adress as you stated.
Anyway, that's the C way of dealing with strings, personally I prefer the C++ way with it's STL string. It's easier to work with than char arrays or pointers, and in case you need a char*, there's the c_str() member function (and to get a char array theres data() and copy() I believe). And usually workability is more important for me than absolute best performance so unless I need otherwise, I work with strings.
You must be logged in to post a response.