[QUOTE=JamesCherrill;1077712]Maybe it's this:
The arg should be a [][] array - which is what the variable args contains.
The invoke method requires an array that contains all the parameters for the method being invoked. The init method has 1 parameter, so the invoke method should pass an array of 1 element, which, in turn, should be your [][] array args. Ie, you need to enclose your [][] args parameter in a new 1 element array.[/QUOTE]

I replaced the line [ICODE]methodInit.invoke(mailTest[i], args);[/ICODE] with [ICODE]methodInit.invoke(mailTest[i], new Object[]{args});[/ICODE] and now it's working fine. Thanks, JC.

Hi,

I'm trying to call a bunch of methods during run-time using reflection, but I'm getting an exception saying "IllegalArgumentException: wrong number of arguments".

Here's some information on the variables used.

  • All elements of mailTestClass[] are classes that extend AbstractTestCase. (FirstTestCase extends AbstractTestCase and FirstTestCase.class is the first element of mailTestClass)
  • These classes have constructors which take a String as an argument.
  • After instantiating a class, init(Object[][] args) method is called using reflection.

The part of the code which creates the objects using reflection works fine. The code blows up when the method init(Object[][] args) is called.

[CODE=java] private void runAll(Class[] mailTestClass, Object[][] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
AbstractTestCase mailTest[] = new AbstractTestCase[mailTestClass.length];
Method methodInit;
Constructor mailTestConstructor;
Class[] constructorParamsClass = new Class[] {String.class};
Integer count[] = new Integer[mailTestClass.length];
Object[] constructorParams = new Object[] {"DummyString"};
int i;
for (i=0; i<mailTestClass.length; i++)
{
try
{
mailTestConstructor = mailTestClass[i].getConstructor(constructorParamsClass);
mailTest[i] = (AbstractTestCase) mailTestConstructor.newInstance(constructorParams);
methodInit = mailTestClass[i].getDeclaredMethod("init", new Class[] {Object[][].class});
methodInit.invoke(mailTest[i], args);//throws the exception here
[/CODE]

The code till line 14 creates an instance of FirstTestCase passing a String "DummyString" to it's constructor.
In line 15, please check if the way the method is invoked is correct. That's the line I'm not sure of.

In FirstTestCase.java:

[CODE=java] public void init ()
{
.
.
.
}

public void init (Object[][] args) // Overloaded methods
{
    .
    .
    .
}

[/CODE]

Thanks. Let me know if additional information is needed.

  1. Construct two matrices.
  2. Declare two 1-D arrays called rowAdd and colAdd to hold the values you obtain when you add the row and column elements respectively.
  3. Once you find the sum of all the row elements and store it in rowAdd, see if the elements of rowAdd are divisible by three. You can do something like this:
    [CODE=c]for(i=0; i<rows; i++)
    {
    if(rowAdd[i] % 3)
    {
    printf("Matrix cannot be formed.");
    break;
    }
    }[/CODE]
  4. If they are all divisible by three, replace the corresponding row in your matrix by the quotient. Using:
    [CODE=c]if(i==rows)
    {
    for(i = 0; i<rows; i++)
    {
    quotient = rowAdd[i]/3;
    for(j = 0; j<cols; j++)
    {
    newMatrix[i][j] = quotient;
    }
    }
    }[/CODE]
  5. Now that you have the desired matrix perform row and column additions again and store them in two new arrays and compute the difference between them and rowAdd and colAdd.

[QUOTE=csurfer;790160]Loops are always better than goto but with goto I think this can be done:

You may also use another variable by the name cnt which would hold the number of times you want to re-run the program in case of a wrong input symbol and alter the code as:

cnt=<number of times you want to run loop>;
----program block----
.
.
.
else // The last else
cnt--;

if(cnt>0)
goto wrong;
[/QUOTE]

That doesn't make much sense. What you said(IF you said what I think you said) can be achieved more conveniently using a loop.

Use int main(), indent your code, use switch cases and a while loop instead of goto and if cases.

Your post gave me an adrenaline rush. Honestly!:icon_eek:

There are quite a lot of mistakes in your program (still!). So instead of pointing out each and every of them, I just whipped up my own code and have offered explanations through comments.

[CODE=c]#include <stdio.h>

include <conio.h>
include <string.h>

int main()
{

char *loginID[] = {"user1", "user2", "user3"};
char *passwords[] = {"pass1", "pass2", "pass3"};
char login[6];
char pass[6], temp;
int i, loginNumber;
printf("Enter the login id\n");
fgets(login,6,stdin); // Use fgets instead of gets or scanf(%s)
for(i=0; i < 3; i++) // Need to see if the entered login ID is any one of the three login IDs
{
    if(!strcmpi(loginID[i],login)) //strcmpi() ignores case
        break;
}
if(i == 3) //If the login ID didn't match in any of the three cases
{
    printf("Invalid login id\n");
    getchar();
    exit(0);
}

loginNumber = i; //need to store the subscript of the login ID
login[6] = '\0'; //NULL terminated string
printf("Enter the password\n");
while((temp = getc(stdin)) != '\n') // flush the input. Google it! Don't use fflush(stdin)
;

for(i=0; (temp = getch()) != '\r'; i++) //Loop executes till 'enter' is pressed
{
    printf("*");
    pass[i] = temp;// store character in pass
}
pass[6] = '\0';
if(!strcmp(passwords[loginNumber], pass))//compare with the appropriate loginID. Use strcmp
{
    printf("\nAccess granted!\n");
    getchar();
    exit(0);
}
else
{
    printf("\nAccess denied!\n");
    getchar();
    exit(0);
}
return 0;

}[/CODE]

The above code is still a li'l crappy since it uses non-portable getch(). I couldn't figure out any other way of getting the password without it being echoed to the screen. Maybe someone else will show a ...

Take a C course in some center in your city. WILL help you out...
I think a C course is a waste of time(even when you've got some to spare). I've learned more from the Internet and Daniweb than I have from my course.

I gave you 3 points on which to work on. You didn't bother to make any corrections in your next post or even ask questions about what I had suggested. There are a lot of other fundamental syntax mistakes too like:
The incorrect if statement syntax,
Redundant braces,
Missing semi-colons,
Illegal use of comma operator,
Use of assignment instead of comparison operator etc.
I would suggest having a look at the 'Starting C' thread.

You gotta use code tags for people to consider answering to your post.
Here's the syntax:

[noparse][code=c]
.
.
your code here
.
.
[/code][/noparse]
Anyway, just a cursory glance through your code showed a few big mistakes:

  1. Use int main()
  2. You are using %c to get a string- should be %s.
  3. Arrays are always passed to functions using pointers (acts as call by reference- sort of).

You can also look up [url=http://en.wikipedia.org/wiki/RSA]RSA[/url] or [url=http://en.wikipedia.org/wiki/Data_Encryption_Standard]DES[/url] if you are dealing with delicate data. But they are both quite complicated to implement (compared to Caesar cipher and rot13).

The if statement is wrong. It should be:

[CODE=cpp]if (pow > 1)
{
full = num * full;
return (power(num, --pow, full)); //You've already multiplied once. Don't do it again.
}[/CODE]

Also, your code breaks when pow = 0. Anyway, it's an easy thing to fix.

Comatose commented: Good Catch +8

As mentioned before, it makes more sense to make sure that the list is in order while inserting than to apply a sorting algorithm later.

You can try this:

[CODE=c]liste_t Insert (liste_t listptr, int val)
{

liste_t *lp = listptr;
liste_t *prev = NULL;
liste_t *info = (struct liste_t *) malloc (sizeof(liste_t));
info->Zahl = val;
info->next = NULL;

if (listptr != 0) 
{
    while (listptr != 0 && listptr->Zahl < val)
    {
        prev = listptr;
        listptr = listptr->next;
    }

    prev->next = info;
    info->next = listptr;[/CODE]

[QUOTE]char dd[1];[/QUOTE]
That's wrong. You're allocating memory for only one character. Should be dd[2].

[QUOTE]aa[] = dd[0] + dd[1];[/QUOTE]
This statement adds the ASCII values of '2' and '3'. So your result will be 50(ASCII value of 2) + 51(ASCII value of 3). And the result can't be just assigned to an entire array.

If you just wanna merge two chars, you can use:
[CODE]aa[0] = dd[0];
aa[1] = dd[1];[/CODE]
But I'm not sure if that's what you're looking for. Maybe more info about what you're trying to do would help.

[QUOTE=ajay.krish123;764846]main() function cannot be called again and again.
Instead you can use another function in place of it or use
[CODE]
goto label;
[/CODE][/QUOTE]

Use of goto is frowned upon. The only place where it's usage is justified is when you're in a nest of loops and wanna come out of it with the least amount of hassle. As Salem has already suggested, loop is the best way to go about this particular problem.

[QUOTE=death_oclock;764152]Umm, that sounded pretty unnecessary. Try:
[CODE=c]printf("row %d sum = %d\n", r + 1, tot);[/CODE]
Keep the loops the same. It will start the labeling at 1 (0 + 1) and go to 3 (2 + 1).[/QUOTE]

I guess that was a li'l too much information, but he wasn't just asking about the labeling(I thought it would be too obvious a thing to ask). I assumed he was talking about array subscripts. Oh well...

[QUOTE=death_oclock;764159]Not entirely true, it is possible that the array was broken up in memory [/QUOTE]
Does that ever actually happen? I thought arrays are [B]always[/B] assigned contiguous blocks of memory.

You can use the [url=http://www.cplusplus.com/reference/clibrary/cassert/assert.html]reference[/url] if you aren't sure of how the assert function works. I think in your program, you've to "assert" that the value of the variable _inode stays within 0 to 3 and this has to be done in the for loop. If the value of that variable is anything other than 0-3, then an error is logged and the program is terminated instead of giving you a segmentation fault.

What's [icode]glblclrtab[/icode] pointing to? It'll have a NULL value or a garbage value which is causing segmentation fault. After you make it point to a character array, you can use [icode]*(glblclrtab+i)= (char) i1;[/icode] or [icode]glblclrtab[i]= (char) i1;[/icode] for simplicity.

By the way, use code tags while posting your code.

You could actually do it by declaring one extra row and column for each matrix that you declare. Then you can make the first row and the first column redundant, then do all the operations on the rest of the array. In this case you could use [icode]for (r = 1; r < =rows; r++)[/icode]. But this is a [B][COLOR="Red"]really really BAD IDEA[/COLOR][/B]. I'm telling you this cause I've found a few textbooks where, for the sake of "simplicity", authors do use the above method(especially in simple programs like bubble sort and the like), but this can cause quite a lot of problems when you start to write much complex programs. Maintaining the array itself can be a big chore, at best. So, even if it seems weird to start looping from zero, get used to it. The alternative is not worth the trouble.

What way did I suggest? If you're talking about starting from row 1 by using the for loop I've "suggested", then you're obviously gonna land in trouble since you're skipping the data in row 0(which is technically the first row). You can't expect to get correct results then; even if you modify the loop to run the exact number of times.

And you can start a row from 1 just by replacing [icode]for (r = 0; r < rows; r++)[/icode] with [icode]for (r = 1; r < rows; r++)[/icode], but you shouldn't do it(
unless you really know what y-ou're doing) since arrays' subscript always starts from 0. If you're not using the loop to access an array, you can use the loop by starting it from 1 and iterate the same no of times, but it's advisable not to.

It was already explained to you by Ancient Dragon in your [url=http://www.daniweb.com/forums/post759666-2.html]previous thread.[/url]

The same program works fine in my compiler (code::blocks using GCC). It prints the line "Sum of all elements = %d" too.

[QUOTE]can you please just explain why you added [i] in those positions within the for loop, because when i replaced my original code with them, i got 4 errors, all the same.[/QUOTE]

[icode]struct sale Weekly_Sale[13];[/icode]
What you're doing here is creating a user-defined variable called Weekly_Sale of type 'struct sale'. It's logically similar to creating arrays of defined types and you use them the same way you would use any other array. That's the reason why you use the subscript [i]. If you're still confused about it, better read up about [url=http://ee.hawaii.edu/~tep/EE160/Book/chap12/section2.1.2.html]arrays of structures.[/url]
[QUOTE]
....the errors said "request for member week in something not a structure or union", and only the bold bit where it says "week" would change to each variable name (i.e. units, price and name) while the rest of the error message would stay the same.
[/QUOTE]
If you copied the code exactly the way it's given by Ancient Dragon, you're bound to get an error since Weekly_Sale isn't the same as weekly_sale [C is case sensitive].

[QUOTE].....would that snippet of code work?[/QUOTE]

Nope. It will if you don't declare the variables as pointers.

You're missing a semi-colon after defining the struct.

Should be:
[CODE]struct names{ // definitions

}[COLOR="Red"];[/COLOR][/CODE]

Firstly, your indentation is pretty bad. I lost the flow at the middle of the code.

Secondly, use int main().

I think this is wrong:
[CODE]if ([COLOR="Red"]cMenu = 1[/COLOR]); // statment for if cMenu is true[/CODE]

You are using an assignment operator instead of '==' operator. The else part of your code will never get executed.

EDIT: Well, Murtan has already said it in a better way. :)

Use code tags and indent your code properly.

Small correction: The variable 'm' doesn't hold the value of the smallest element, but it holds the index of the smallest element in the array.