Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 25th, 2013, 06:45 AM
 karthikslegend
Registered User

Join Date: Jun 2013
Location: Chennai , India.
Posts: 1
Time spent in forums: 23 m 11 sec
Reputation Power: 0
What's the mistake i'v did in this?

Code:
```#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int i,j;
int func1(int x,int y,int z);
int func2(int x,int y);
void main()
{
clrscr();
struct
{
int a;
int b;
}s1[3][3],s2[3][3],s3[3][3],s4[3][3];
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
cin>>s1[i][j].a;
}
}
for(i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
cin>>s1[i][j].b;
}
}
for(i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
s1[i][j]=s2[i][j];
}
}
for(i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
cout<<func1(s2[i][j].a,s2[i][j].b,s3[i][j].a);
}
}
getch();
}
int func1(int x[3][3],int y[3][3],int z[3][3])
{
for(i=0;i<=3;i++)
{
for(j=0;j<=3;j++)
{
z[i][j]=x[i][j]+y[i][j];
}
}
return z[i][j];
}
//int func2(int x,int y)
//{
//	return x*y;
//}
```

#2
June 25th, 2013, 07:00 AM
 ptr2void
I haz teh codez!

Join Date: Dec 2003
Posts: 2,530
Time spent in forums: 1 Month 2 Weeks 3 Days 18 h 48 m 26 sec
Reputation Power: 2336
First mistake: cross-posting.

Then there's stupid single-letter variable names, not to mention probably using Turbo C++.
__________________
I ♥ ManiacDan & requinix

This is a sig, and not necessarily a comment on the OP:
Please don't be a help vampire!

#3
June 25th, 2013, 10:21 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,856
Time spent in forums: 3 Months 1 Day 5 h 8 m 22 sec
Reputation Power: 2199
Your mistake is blatantly obvious to even the most casual observer. You did not ask an actual question.

Last edited by dwise1_aol : June 25th, 2013 at 11:18 AM.

#4
June 25th, 2013, 11:22 AM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,856
Time spent in forums: 3 Months 1 Day 5 h 8 m 22 sec
Reputation Power: 2199
Another big mistake you're making is that you are ignoring warnings:
Quote:
 C:>g++ -Wall karthik1.cpp karthik1.cpp:8: return type for `main' changed to `int' karthik1.cpp: In function `int main(...)': karthik1.cpp:22: warning: name lookup of `i' changed karthik1.cpp:4: warning: matches this `i' under current ANSI rules karthik1.cpp:15: warning: matches this `i' under old rules karthik1.cpp:14: warning: unused variable `struct main(...)::{anonymous} s4[3][3]' C:>

Never ignore warnings! Warnings are far more important than error messages are. Warnings are telling you that your code is broken. Never ignore warnings!

And if you think that there's a problem with your code, then tell us! Don't make us guess, but rather tell us what makes you think that there's a problem. Describe the symptoms just as you would do with your physician. You wouldn't go to your physician and make him guess your symptoms instead of describing them to him, so why do that with us?

PS
Though I don't understand why the compiler didn't complain about your not having actually created the function, int func1(int x,int y,int z). You did create another different function by the same name (function overloading), but not the func1 that you are calling.

int func1(int x,int y,int z);
is not the same as
int func1(int x[3][3],int y[3][3],int z[3][3])

PPS
Another mistake is that there are no comments. We cannot even figure out what you're trying to do in that code. Nor what you think that func1 is returning. Without any idea of your intentions, we could not even begin to suggest remedies.

Last edited by dwise1_aol : June 25th, 2013 at 11:52 AM.

#5
June 25th, 2013, 01:03 PM
 dwise1_aol
Contributing User

Join Date: Jan 2003
Location: USA
Posts: 6,856
Time spent in forums: 3 Months 1 Day 5 h 8 m 22 sec
Reputation Power: 2199
Just from a quick glance, I see these problems:

1. You are declaring main as void even though main is supposed to return int. Always! So declare it as returning int and have the last line in main return zero.

2. Never mix stdio/conio with iostreams! And most definitely not the input functions! They have their own different methods for handling input which are very likely to interfere with each other.

So replace that getch() with the appropriate cin method.

3. Do not declare i and j as globals, but rather declare them locally in each function that uses them.

Example of a possible problem that global i might be causing:
Look at the last nested for loop in main. The compiler is confused on this, but it looks like the i that it's using is the global i. func1 is definitely using the global i. So as main is trying to iterate i from 0 to 3, each call to func1 is setting i to 4! For main, i keeps getting changed out from under it! It looks to me like func1 only ever gets called once or maybe 4 times, but certainly not the full 16 times that you think that you're calling it.

Give func1 its own local i and j variables so that it won't interfere with other loops in other functions. For that matter, get rid of those global variables altogether and either give main its own local i and j, or else give each for loop its own i and j, but be consistent (some of the warnings about i are because you are being inconsistent with the for i loops).

4. Yes, I did indeed say 16. Not 9, but 16. Because you keep telling each for loop to iterate from 0 to 3, which is four times! You are declaring the arrays to be 3×3, but you keep treating them as if they were 4×4. Which is it? If you want to treat those arrays as 4×4, then declare them as 4×4! If you truly want them to be 3×3, then stop treating them as if they were 4×4! Make up your mind!

5. By declaring your arrays as 3×3 and then treating them as 4×4, that means that you are overflowing your arrays and overwriting memory that doesn't belong to those arrays. If those memory locations are being used by other variables or control values (ie, values used by the function to do its job), then that means that you are clobbering those other variables or control values. Clobbering causes all kinds of weird and "inexplicable" problems that will drive you crazy.

The solution is to never overflow any array. Never! That means that if you declare an array to be 3×3, then treat it as a 3×3 array!

6. C++ provides a handy feature called overloading which can be applied to functions and operators. That means that you can reuse the same function name for several different functions. What distinguishes one overloaded function from another (which the compiler must do in order to know which one to call) is that their parameter lists must all be different from each other. Overloading also means that you need to actually create each of those different overloaded functions.

int func1(int x,int y,int z);
takes three arguments, each one of which is an int value.
int func1(int x[3][3],int y[3][3],int z[3][3])
takes three arguments, each of which is a 3×3 array of int.
A 3×3 array of int is not the same thing as a single int, so these are two different functions.
In main, you make this call to func1:
cout<<func1(s2[i][j].a,s2[i][j].b,s3[i][j].a);
Since each argument is a single int value, you are calling the first func1. But you never create that version of func1, but only the version that takes three arrays!

If you are going to use overloading, then complete the job! If you did not want to use overloading, then fix the problem! Decide just what you want to pass to func1 and apply that decision! The prototype, function definition, and function call must all match each other in the data types of the parameters/arguments!

7. In the func1 that you do create, what is this supposed to be?
return z[i][j];
You do realize that the values of i and j are both four! z is declared as a 3×3 array, so z[4][4] does not exist! You are returning garbage!

For that matter, z[3][0], z[3][1], z[3][2], z[0][3], z[1][3], z[2][3], and z[3][3] also do not exist. So when you were writing values to those locations, you were yet again clobbering what was there. If you happened to have clobbered one of the function's control values, then you would very likely have caused the program to crash. Did it crash? We don't know, because you want to make us play guessing games with you.

Last edited by dwise1_aol : June 25th, 2013 at 05:03 PM.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > What's the mistake i'v did in this?