Reversing An Array
Reversing An Array
The following program is what i have typed up, but I have 2 issues. This compiling issue in line
4:
4:23: error: expected unqualified-id before 'int'
4:23: error: expected ')' before 'int'
Also, I tried to write the function with 20 numbers, but I don't know how to make it be able to
take unlimited numbers in the array. Any help is appreciated, Thank you!
#include<iostream>
using namespace std;
}
int main()
{
int temporary, arraylength;
float array[50];
cout << "Enter the array size: ";
cin >> arraylength;
}
Apr 21, 2016 at 1:43pm
integralfx (897)
Please use code tags.
http://www.cplusplus.com/articles/jEywvCM9/
http://cpp.sh/3nl56
Last edited on Apr 21, 2016 at 1:43pm
Apr 22, 2016 at 1:48am
brendancostner (16)
Well sir, you are awesome. I feel like an idiot not trying that as a void. I have been confused on
which to use, and what time. My teacher has gone over functions where he will use void, struct,
string, float, etc. and I have no clue when to you which. Do you by any chance know the
differences?
Apr 22, 2016 at 4:41am
Yawzheek (120)
My teacher has gone over functions where he will use void, struct, string, float, etc.
A struct is just a user-defined type that you'll create yourself, basically. It's just a class with
public members by default. You know how you make something and use it, like:
1 int i = 0;
2 cout << i << endl;
int is a TYPE, and struct allows you to use your own user TYPE.
Float is just floating point numbers. You're probably more familiar with "double." It too is just a
type.
String is also a type, and it's used for strings (hence the name). You use that for words/text:
1 string s1 = "Hello";
2 string s2 = "world!";
3 cout << s1 + ' ' + s2 << endl;
void is a "type" in the sense that it doesn't return anything. It's quite useful, especially for
functions where you want to do something to something else, but (obviously) have no need or
desire for a return type. You could use a void function, using the variables s1 and s2 above, in a
manner such as:
So hopefully now you see that I could create any number of strings and concatenate them (put
them together two at a time -- "stitch" them together, if you will) by simply passing two strings
to the function print(), rather than typing cout instructions every time I wanted to do it. No return
value is needed for this, so we use void.
4
1
I'm writing a program that has a user input integers into an array, calls a function that removes
duplicates from that array, and then prints out the modified array. When I run it, it lets me input
values into the array, but then gives me a "Segmentation fault" error message when I'm done inputing
values. What am I doing wrong?
Here is my code:
#include <iostream>
cout << "Please enter a series of up to 100 integers. Press 'q' to quit. ";
rmDup(values, currentSize);
return 0;
}
Thank you.
/*
Program - Array Sorting
Author - Vishal Solanki
Language - C Language
Date - 03/02/2018 (dd/mm/yyyy)
*/
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (int i = 0; i < n; i++) //Loop for
ascending ordering
{
for (int j = 0; j < n; j++) //Loop for
comparing other values
{
if (a[j] > a[i]) //Comparing other
array elements
{
int tmp = a[i]; //Using temporary
variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nAscending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing
array data after sorting
{
printf(" %d ", a[i]);
}
for (int i = 0; i < n; i++) //Loop for
descending ordering
{
for (int j = 0; j < n; j++) //Loop for
comparing other values
{
if (a[j] < a[i]) //Comparing other
array elements
{
int tmp = a[i]; //Using temporary
variable for storing last value
a[i] = a[j]; //replacing value
a[j] = tmp; //storing last value
}
}
}
printf("\n\nDescending : "); //Printing message
for (int i = 0; i < n; i++) //Loop for printing
array data after sorting
{
printf(" %d ", a[i]); //Printing data
}
return 0; //returning 0
status to system
}
//Ouput
/*
Array size: 10
Elements : 3 4 7 6 5 1 2 8 10 9
Ascending : 1 2 3 4 5 6 7 8 9 10
Descending : 10 9 8 7 6 5 4 3 2 1
*/
import
java.util.Random;
/**
This class generates permutations of a sequence of integers
1...length.
*/
public class PermutationGenerator
{
private Random generator;
private int length;
/**
Construct a PermutationGenerator object.
@param length the length of the permutations generated
by this generator.
*/
public PermutationGenerator(int length)
{
// "Seeding" this so testing is deterministic.
generator = new Random(42);
this.length = length;
}
/**
Gets the next permutation.
@return the array containing the next permutation
*/
public int[] nextPermutation()
{
??????
}
}
Raw
PermutationTester_Skeleton.java
import java.util.Arrays;
/**
Prints some permutations.
*/
public class PermutationTester
{
public static void main(String[] args)
{
PermutationGenerator gen = new PermutationGenerator(10);
System.out.println(Arrays.toString(gen.nextPermutation()));
System.out.println("Expected: [5, 7, 3, 2, 8, 10, 9, 6, 4, 1] ");
System.out.println(Arrays.toString(gen.nextPermutation()));
System.out.println("Expected: [5, 4, 7, 2, 6, 8, 1, 10, 9, 3] ");
System.out.println(Arrays.toString(gen.nextPermutation()));
System.out.println("Expected: [7, 10, 8, 9, 2, 6, 3, 4, 5, 1] ");
System.out.println(Arrays.toString(gen.nextPermutation()));
System.out.println("Expected: [7, 5, 8, 10, 3, 6, 9, 2, 4, 1] ");
int[] nextPermutation
PermutationGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Random;
/**
This class generates permutations of a sequence of
integers
1...length.
*/
public class PermutationGenerator
{
. . .
/**
Construct a PermutationGenerator object.
@param length the length of the permutations
generated
by this generator.
*/
public PermutationGenerator(int length)
{
. . .
}
/**
Gets the next permutation.
@return the array containing the next permutation
*/
public int[] nextPermutation()
{
. . .
}
}
Use the following file:
PermutationPrinter.java
/**
This class prints 5 permutations of the numbers 1 through 10.
*/
public class PermutationPrinter
{
public static void main(String[] args)
{
PermutationGenerator gen = new PermutationGenerator(10);
Dsa