d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Explain What Is Wrong With This (c++)
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 04:10pm
Code
arr2 = new int[20];
for (int i = 0; i<20;i++) {
if (i < 10)
arr2[i] = arr1[i];
else arr2[i] = 0; }


figured out #2 after getting some food in me (thinking power ftw)

still working on the first one.

they say pointers plurally so I assume they want me to not use new int[100] (this would be a single pointer to the location of 100 allocated ints starting at the first position right)

mad confusing wording.

This post was edited by Eep on Aug 30 2012 04:14pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Aug 30 2012 04:18pm
Post the setup code, if you have it. I think I misinterpreted the setup from working backwards from your code, so I'm kind of guessing at the setup a little bit here.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 04:21pm
Quote (irimi @ Aug 30 2012 05:18pm)
Post the setup code, if you have it.  I think I misinterpreted the setup from working backwards from your code, so I'm kind of guessing at the setup a little bit here.


this is why the website sucks


Allocate an array of 100 integer pointers and assign the resulting pointer to the appropriately declared variable, ip_arr. Allocate 100 integers and assign the resulting pointers to the elements of ip_arr. Initialize each integer value to -1.


that is the question

the compiler, I checked, all it had was main { return 0; }


so you have to do everything I am guessing.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 30 2012 05:08pm
my biggest issue is when I stop getting compiler errors, the website keeps telling me I allocated the wrong number of elements. Like I did 100 fucking loops and I am pretty sure I understand how arrays work by the end of cs1250.

Code
int *ip_arr[100];
int *a = new int[100];
for (int i = 0; i < 100; i++)
{
a[i] = -1;
ip_arr[i] = a+i;
}


is this not filling every (100) elements up?

this website is like a shitty person who tells you to make something and then bitches when you do exactly as they say.

This post was edited by Eep on Aug 30 2012 05:11pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 31 2012 02:03am
I'm convinced the questions wording is a typo or just complete horse shit. I've tried to understand it from every angle.

I don't know why they insist on this cryptology for the assignments. Just list, in order, what you want me to do. Because if some client ever gave me a thing he wanted built and described it like MPL I would set his car on fire.

This post was edited by Eep on Aug 31 2012 02:07am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 31 2012 04:10am
maybe your only initializing 0 - 99?

test code:

Code
int main()
{
   int i = 0;
   for(i = 0; i < 10; i++)
   {
       printf("%d\n", i);
   }
   return 0;
}


returns:

Code
0
1
2
3
4
5
6
7
8
9

Process returned 0 (0x0)   execution time : 0.020 s
Press any key to continue.


:3 take a minute and look at it not sure if it will help. maybe try using a <= clause.

fuck its 3 am been playing to much guild wars. iirc without this lack of sleep in mind a[100] only goes from 0->99 anyways so disregard this post :/ lol

edit:: maybe it wants you to create an array of pointers and pass the memory address of each element of integers to the pointer.

ex:

Code
//Allocate an array of 100 integer pointers and assign the resulting pointer to the appropriately declared variable, ip_arr. Allocate 100 integers and assign the resulting pointers to the elements of ip_arr. Initialize each integer value to -1.
int main()
{
   int *ip_arr[100]; //allocate a array of 100 integer pointers and name it ip_arr
   int a[100]; //allocate 100 integers and assign their memory location to the 100 pointers
   int i = 0;

   for(i = 0; i < 100; i++)
   {
       a[i] = -1; //allocate the interger to -1
       ip_arr[i] = &a[i]; //send the address of the current element to the pointer array
   }

   for(i = 0; i < 100; i++) //some debug stuff you can remove. just wanted to test to make sure each pointer element had the memory address of the integer.
   {
       printf("%d\n", *ip_arr[i]);
   }

return 0;
}


could also want you to allocate the -1 intger through the pointer array.

ex:

Code
for(i = 0; i < 100; i++)
   {
       ip_arr[i] = &a[i];
       *ip_arr[i] = -1;
   }


simple mod to see if it actually writes the memory addresses. iirc that would essentially set a[i] to -1 through the pointer.

Code
#include <stdio.h>
#include <stdlib.h>

int main()
{
   int *ip_arr[10];
   int a[10];
   int i = 0;

   for(i = 0; i < 10; i++)
   {
       a[i] = i + i;
       ip_arr[i] = &a[i];
   }

   for(i = 0; i < 10; i++)
   {
       printf("%p: %d\n", &ip_arr[i], *ip_arr[i]);
   }

return 0;
}


result:

Code
0028FEF4: 0
0028FEF8: 2
0028FEFC: 4
0028FF00: 6
0028FF04: 8
0028FF08: 10
0028FF0C: 12
0028FF10: 14
0028FF14: 16
0028FF18: 18

Process returned 0 (0x0)   execution time : 0.017 s
Press any key to continue.


This post was edited by AbDuCt on Aug 31 2012 04:36am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 31 2012 12:27pm
I'll try what you mentioned, thanks again abduct
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Aug 31 2012 04:19pm
still didn't work. It is a problem with the website. Has to be.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Aug 31 2012 05:55pm
Quote (Eep @ Aug 31 2012 06:19pm)
still didn't work. It is a problem with the website. Has to be.


i dont see how a website can verrify code. when coding there are a number of ways to do the same thing as is. if you combine that with sketchy questions how on earth are you supposed to get the input it wants lol
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Sep 1 2012 03:51am
Quote (AbDuCt @ Aug 31 2012 06:55pm)
i dont see how a website can verrify code. when coding there are a number of ways to do the same thing as is. if you combine that with sketchy questions how on earth are you supposed to get the input it wants lol


beats me :/

I can't find any information about the websites questions on the web either. Must be highly filtered or some shit.

Either way, shit product and shit site. Sucks I have to do homework through them this semester.
Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll