[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 5 KB, 265x190, images.jpg [View same] [iqdb] [saucenao] [google]
2027319 No.2027319 [Reply] [Original]

the question is write a program that declares an array named alpha with 50 components of the type double. Initialize the array so that the first 25 components are increased by 3 and the last 25 components are increased by 12. Output the array so that the ten elements per line are printed.

>> No.2027322

Get out.

>> No.2027325

All in the same loop?

>> No.2027343

no i think they have to be different loops

>> No.2027471

That sounds pretty easy OP, if you've been paying attention in lectures and doing the practicals you should find it a doddle.

>> No.2027483

> C++
LOOOOOOOOOOOOOOOOOL

>> No.2027493

int alpha[50] = {0};

for ( int x = 0; x < 50; x++ ) {

if ( x <= 25 ) { alpha[x] = x + 3; }
else if ( x > 25 ) { alpha[x] = alpha[25] + 12 + alpha[x]; }

}

>> No.2027500

>>2027493
else if ( x > 25 ) { alpha[x] = alpha[25] + 12 + alpha[x - 1]; }

my bad

>> No.2027502

this is not even homework

>> No.2027506

Pseudo:
Set alpha
Populate alpha
Set i = 0
While i <= 24
alpha[i] = alpha[i] + 3
i++
End Loop
Set i = 25
While i <=49
alpha[i] = alpha[i] + 12
i++
End loop

Echo alpha[0-9]
Echo alpha[10-19]
Echo alpha[20-29]
Echo alpha[30-39]
Echo alpha[40-49]

>> No.2027513

#include <iostream>


int main( ) {

int alpha[50] = {0};

for ( int x = 0; x < 50; x++ ) {

if ( x <= 25 ) { alpha[x] = x * 3; }
if ( x > 25 ) { alpha[x] = alpha[x -1] + 12; }

}

for ( int a = 0; a < 50; a++ ) {

std::cout << alpha[a] << "\n";

}

std::cin.get( );
return 0;

}

I think that's right.

>> No.2027518

>>2027513
This is correct.

>> No.2027523

array<double,50> alpha;
for (int i=0;i<25;i++)alpha[i]=3;
for (int i=25;i<50;i++)alpha[i]=12;
for (int i=0;i<50;i++)
{
cout << alpha[i];
if (!i%9)cout << endl;
}

>> No.2027527

I don't write C++. It's dirty. Here's some C code

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

int main(int argc, char** argv){
    int alpha[50], i;

    // initialize alpha
    alpha[0] = 0;
    for(i=1; i < 25; i++)
        alpha[i] = alpha[i-1]+3;
    for(i=25; i < 50; i++)
        alpha[i] = alpha[i-1]+12;
    
    // print alpha
    for(i=0; i < 50; i++){
        if((i%10)==0)
             fprintf(stdout, "\n");
        fprintf(stdout, "%d ", alpha[i]);
    }
}

>> No.2027529

>>2027527
C is dirty, that's what it is.

>> No.2027528

(define (make-adder n) (λ(x) (+ n x)))
(define 3+ (make-adder 3))
(define 12+ (make-adder 12))
(define (zero n) 0)
(append (map 3+ (build-list 25 zero)) (map 12+ (build-list 25 zero)))

>> No.2027541

>>2027529

your face is dirty.

also, do people not indent anymore? why am I the only one who indents and comments. Kids these days.

>> No.2027546

>>2027541
There's no point commenting an obvious piece of code. I can't be arsed enough to have this board retain my formatting.

>> No.2027556

You have a home.
>>>/prog/
Now get this fucking shit off my sci

>> No.2027582

#include<vector>


int main()
{
std::vector<double> v(50,0.0);
std::vector<double>::iterator it;
for(it=v.begin(); it!=v.last(); it++)
{
if(it<v.begin()+25)
(*it)+=3;
else
(*it)+=12;
}
return 0;
}

>> No.2027590
File: 172 KB, 374x333, wtfreading.jpg [View same] [iqdb] [saucenao] [google]
2027590

>>2027582

>> No.2027597

>>2027582
> implying vectors are arrays

>> No.2027601

>>2027597
>Implying they aren't superior in every way and function identically
>Implying vectors shouldn't be used in place of an array whenever possible

>> No.2027606

>>2027601
>Implying vectors wouldn't turn into a nightmare if you were trying to memory align your data structures.

>> No.2027610

>>2027601
> implying anyone interested in superiority uses C++

>> No.2027611
File: 25 KB, 430x309, rofl-8.jpg [View same] [iqdb] [saucenao] [google]
2027611

>>2027606
>Doesn't know how to define custom iterators

>> No.2027614

>>2027610
>implying they don't

>> No.2027618

>>2027610
>Every major OS Kernel written in C or C++
Status:
[x] Told
[  ] Not Told

>> No.2027619

>>2027611
>Implying iterators have anything to do with vectors because they are just linked lists and each vector item is placed arbitrarily in memory.

>> No.2027623

>2027610
I am Google and what is this

>> No.2027624
File: 97 KB, 397x295, fr-2.jpg [View same] [iqdb] [saucenao] [google]
2027624

>>2027619
>Doesn't even know an iterator can be subdefined for a class to work however he wants

>> No.2027628

>>2027527
Your mom vagina is dirty.

>> No.2027638
File: 3 KB, 209x214, jelly.png [View same] [iqdb] [saucenao] [google]
2027638

>>2027618
> mfw future operating systems are largely written in safe languages in the future and C and C++ are relegated to the dustbin where they belong
Even MS knows that C and C++ are huge fucking problems (singularity research, powershell, etc).

>> No.2027643

>>2027619
> vector
> not O(1) access
dude wat

>> No.2027657

>>2027624
>Uses abuse of the term 'iterator' to mean shit that it doesn't mean because an iterator is just a pointer used to ITERATE through a list of objects and you CAN NOT modify a list with an iterator before you are done iterating through it because it will invalidate the iterator and making a copy of the list and using an iterator to change it is retarded and poor practice.

>> No.2027660

>>2027638
>future operating systems are largely
>future... are largely
>future...are
No solid OS/Drivers/Engines or any backbone software will ever be written in anything high level, ever.

They are just too slow and hardware dependent.

>> No.2027661

>>2027657
Let me see if I understand this.
> functional programming is dumb

>> No.2027668

2027513 is best

since u guys enjoy this so much i have another one for you.

In C++. write a program that delcares an array named myArray with 8 components of the type double. Initialize the array to 8 values that the user will input. Finally, pass the array as a parameter to a new function called printAllElements. This function will display values of each element in the array. You must use a separate function to print the elements.

>> No.2027672
File: 47 KB, 190x154, ralph.png [View same] [iqdb] [saucenao] [google]
2027672

Java master race here.

public class Array {
public static void main(String[] args) {
double[] alpha = new double[50];
for(int i = 0; i<24; i++)
alpha[i] = alpha[i] + 3;
for(int i = 24; i<50; i++)
alpha[i] = alpha[i] + 12;

for(int i = 0; i<10; i++)
System.out.print(alpha[i]+" ");
for(int i =10; i<20; i++)
System.out.print(alpha[i]+" ");
for(int i = 20; i<30; i++)
System.out.print(alpha[i]+" ");
for(int i = 30; i<40; i++)
System.out.print(alpha[i]+" ");
for(int i = 40; i<50; i++)
System.out.print(alpha[i]+" ");
}
}

>mfw at for loops for printing

>> No.2027674

I am 1 what is this?

>> No.2027675

>>2027657
>you CAN NOT modify a list with an iterator before you are done iterating through it
Yes you can, and if you know your ass from a hammer when it comes to programming you can do it very easily without any adverse effects.

Iterators are just a replacement for array indexes that manages memory and traversal properly. You just have a pre-conceived notion of how they should work and are probably to scared/stupid to realize that they can be used many, many different ways.

>> No.2027681

>>2027660
> doesn't know about the proliferation of erlang in telecoms industry
Don't mistake the stupidity and intertia of the weakly-typed, buggy, overly-verbose, low-level shit languages for merit.

>> No.2027684

> uses iterators on vectors
> turns vectors into lists
[ ] Brilliant
[X] Derp

>> No.2027688
File: 11 KB, 295x290, cplusplus.jpg [View same] [iqdb] [saucenao] [google]
2027688

>> No.2027694

>>2027668
#include<iostream>
using namespace std;

void printAllElements(double a[]; int size())
{for(unsigned int i=0; i<size; i++){cout <<a[i]<<' ';}}

int main(){
double myArray[8]; int i=0; double temp=0;
while(cin >> temp)
{if(cin.good()){ myArray[i++]=temp;} cin.clear()}}

>> No.2027695
File: 170 KB, 1200x2250, Cross.jpg [View same] [iqdb] [saucenao] [google]
2027695

Binary

>> No.2027696

>>2027668

#include <iostream> int main() { cout << "i want something more serious than that easy shit"; }

>> No.2027702

Another one:

#include <iostream> int main() { while() cout << "ENDLESS LOOP OF HATE"; }

>> No.2027704

>>2027696
Make a function that, when called with an integer n, creates a function that takes an integer m, and returns m+n.

No overflow.

>> No.2027706

>>2027696
I suck at C++ a lot of people do don't feel bad.

have you tried math? make sure to include the math library

#include <cmath>

when you installed your C++ compiler it should have come with most of the libraries.

>> No.2027709

>>2027684
You don't even know what you are talking about anymore, nor did you to begin with I think.

Well designed iterators for an ADT can function however they need to, you just don't/can't understand that. I use customer iterators for matrix storage all the time because nested vectors are impossible to traverse/modify with normal indexes. Not to mention I can set my own traversal methods up that suite what I'm doing, not just straight O(n) shit.

Also...
>linked lists
>Implying you even know how iterators work

>> No.2027710

>>2027704
C++ doesn't have nested functions, retard.

>> No.2027712

If you ever have to take a memento into society that you will remember, it's the cross. When you do something evil knowingly for the greater good, it's because of the cross.

>> No.2027713

>>2027710
>Doesn't know about function pointers.

>> No.2027714

>>2027710
that isn't a nested function...

>> No.2027719

>>2027527
C++ would be good if Microsoft didn't bastardize every fucking thing with marketing .NET BULLSHIT

everything Microsoft has ever touched it turned to piss and shit, still better than mac tho I suppose.

but only slightly better, it's not a oh my god better it's "this will just fucking do" better.

>> No.2027727

>>2027675
http://www.rhinocerus.net/forum/language-c-moderated/603243-deleting-vector-while-iterating-allowed..
html

herpa derpa durr

>> No.2027736

>>2027672
what good is Java?

I'm sure its good for something...

>> No.2027737

Why did we talk in the first place?
Snake comes in many forms I guess,
Then we are one of the kinds that ~can~ do the greater good. To commit the first good, even unintentionally requires a snake... ssss ~~
When you see the snakes eyes you turn to stone. An epiphany ~!

>> No.2027739

Then you carry the cross. -+

>> No.2027747
File: 106 KB, 489x400, retard-7.jpg [View same] [iqdb] [saucenao] [google]
2027747

>>2027727
>default iterators
>link doesn't even support what you're trying to say
>maximum_full_retard.jpg

>> No.2027751
File: 285 KB, 1024x768, n31.jpg [View same] [iqdb] [saucenao] [google]
2027751

And I'm far from a retard

>> No.2027764
File: 75 KB, 696x282, repo.png [View same] [iqdb] [saucenao] [google]
2027764

>>2027751
That may or may not be true, what I know is you are reported.

>> No.2027767

In C++. write a program that delcares an array named myArray with 8 components of the type double. Initialize the array to 8 values that the user will input. Finally, pass the array as a parameter to a new function called printAllElements. This function will display values of each element in the array. You must use a separate function to print the elements.

#include<iostream>
using namespace std;

void printAllElements(double a[]; int size())
{for(unsigned int i=0; i<size; i++){cout <<a[i]<<' ';}}

int main(){
double myArray[8]; int i=0; double temp=0;
while(cin >> temp)
{if(cin.good()){ myArray[i++]=temp;} cin.clear()}}

i dont think thats right

>> No.2027769

Imagine passing money over a table, that is the cross.

>> No.2027776

>>2027767
I think that you shouldn't be posting your homework problems that could be solved by someone who has had their brain replaced with a nerf football.

>> No.2027780

double alpha[50]={0,3,6,9,12,15,18,21,24,27,30,33,36,39
,42,45,48,51,54,57,60,63,66,69,72,75};
for int (i=25;i<50;i++)
{
alpha[i]=75+i*12;
}

>> No.2027779

>>2027769
>>2027751
>>2027737
>>2027739
>>2027712
>>2027695
Very poor attempt at religious trolling, even the trip doesn't fit.

>> No.2027788

>>2027776
he's a beginner you nigger we all were there once.

>> No.2027792

>>2027788
Maybe you where, I learned coding over a weekend and a case of beer.

>> No.2027803

>>2027792
you cannot into circuitry though :p

thus you may be able to into assembly not will never into WHY?

>> No.2027821

>>2027803
I know assembly(both MIPS and X86), Java, C/C++/C#, VB(sadly), Lisp(and Scheme variant), Perl, and Ruby. Touched on Cobol, but I can hardly say I know it.

>> No.2027827

>>2027803
>>2027821
As an aside, I have a Math/CS degree. I taught myself coding in HS while I was bored one weekend.

>> No.2027836

>>2027827
but are you white?

>> No.2027851

>>2027827
About as white as I can be without being albino.

>> No.2027862

>>2027709
Custom iterators are a poor substitute for a proper typing system and/or a proper method of extending syntax. Unfortunately C++ programmers are only now beginning to realize these things, as C++0x takes them marginally closer to the state of programming languages in 1975.

It is really illuminating to hear C++ programmers talk about the issues they face and their means of handling them. Don't get me wrong, I don't hate C++ programmers, I just hate C++.

>> No.2027865

>>2027851
fine you win.

>> No.2027883

>>2027862
>poor substitute for...etc
You still aren't getting it, you do all that in the iterators themselves. You are still stuck in the rut that makes you think iterators have to be used for a linear traversal, they don't.

Not to mention, properly implemented custom iterators make code orders or magnitude easier to read.

>> No.2027881

>>2027821
Only two thoughts come to mind when someone says they know C++.
1) They're probably lying.
2) They may not know they're not lying.

>> No.2027901

>>2027881
One of my graduate level courses was the implement a C++ compiler based on the(retardedly stupid and large) CFG~ grammar. So I probably understand it as well as is possible to.

>> No.2027920

>>2027672
You don't know SHIT about Java. I bet your code doesn't compile because Array is already defined in System or something.

And why is a homework class public?

>> No.2027925

>>2027657
>he still doesn't know how iterators work
>javafag detected

also, >>>/prog/

>> No.2027946

>>2027925
They don't actually do any coding on /prog/.

It's a small consequence of being a text board that gets no traffic.

>> No.2027954

i haven't used STL that much, but a brief look at The Elements of Programming makes me think Stepanov really knew his shit.

>> No.2027958

>>2027883
> iterators extend C++ syntax
no, dude, they don't

>> No.2027960

it's funny how all the c++ threads show up around this time of the semester. shit gets real after the 2nd exam =/.

>> No.2027968

>>2027901
Oh? How did you solve the halting problem?

>> No.2027973

>>2027767
void printAllElements(const double * a, unsigned int num_elements)
{
for (unsigned int i = 0; i < num_elements; ++i)
{
cout << a[i] << endl;
}
}


int main()
{
const unsigned int num_elements = 8;
double myArray[num_elements];
double input = 0;

your_input_shit;

printAllElements(myArray,num_elements);


}

>> No.2028142
File: 185 KB, 1600x900, x20.jpg [View same] [iqdb] [saucenao] [google]
2028142

It was just a good guess.