C/C++ :: Reversing Array Contents

Feb 21, 2015

I'm trying to reverse 25 items that are in my array by using a For Loop and sending 2 of the numbers to a function at a time. The code I have right here, is not working at the moment, I cannot find the problem because i've been staring at my code for too long.

int farEnd = 24;
for (int i = 0; i < 24; i++){
Swap(intArray[i], intArray[farEnd]);
farEnd--;
}

Also here is the code in my function

void Swap(int num1, int num2){
int temp = 0;

temp = num1;
num1 = num2;
num2 = temp;
}

View 5 Replies


ADVERTISEMENT

C :: Reversing Elements In Array

Apr 15, 2014

Write the definition of a function reverse , whose first parameter is an array of integers and whose second parameter is the number of elements in thearray . The function reverses the elements of the array . The function does not return a value .

Code:
void reverse(int a[], int num) {
for ( int i=0; i <= num/2 ; i++){
int temp = a[i];
a[i] = a[num-i-1];
a[num-i-1] = temp;
} }

This is supposed to be the answer but I'm not quite sure why this is. I understand everything up until the actual loop. For one, shouldn't "int i" be declared outside the loop (I thought perhaps this was an error in the solutions)?

The main thing that I do not understand is the conditional statement.

Code: i<=num/2;

I don't understand why the "num/2" is necessary here. Also I can't really remember but is there a command that actually reverses an array?

View 5 Replies View Related

C/C++ :: Reversing A String / Char Array

Aug 12, 2013

I'm new in the C programming language, so I tried to create a program that reverses a string. This is my code:

/* Reverse string */
#include <stdio.h>
#include <string.h>  
int main() {
  char s[8]="Welcome";  
 
[Code] ....

The output of the program is "@".

View 5 Replies View Related

C/C++ :: Reading A File Into Array Then Reversing The Array

Jan 19, 2015

This is what I am supposed to be doing with this problem: Read the integer values in the file "numbers.dat" and display

* them in reverse order (that is, display the last number in the
* file first, the second-to-last second and so on). Use an array to
* store the values. It is safe to assume that the file contains no
* more than 100 values.

I have gotten far enough to read the file and display as an array, but it is displaying vertically rather than horizontally. So it is displaying 10 and a 1 on the first line then the 0 on the second line. Before I can work the reverse part out, I need it to display each number as 10 line 1 -235 line 2 so on and so forth.

This is my code so far:

void display_array_reversed() {
ifstream fin ("numbers.dat");
if (!fin){
cerr << "Error opening file" << endl;
exit(1);

[Code] .....

View 2 Replies View Related

C++ :: Char Array Reversing Function Using Pointer?

Feb 4, 2014

Why does this code doesnt work?

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
class my_string {
char* ptr;

[code] ....

View 1 Replies View Related

C/C++ :: Printing The Contents Of The Array?

Feb 18, 2014

I am writing a code with C where I will give an integer and get the binary conversion of it.I tried to fill the binary digits into an integer array.But when I normally print it will give the proper output.But when I try to print the contents of the array it will not produce the proper result.

My code would be as follows.

#include <stdio.h>
#include <conio.h>
int main()

[Code]....

View 5 Replies View Related

C++ :: Display Contents Of Two-Dimensional Array

May 4, 2014

//Introductory20.cpp - displays the contents of a two-dimensional array, column by column and row by row

#include <iostream>
using namespace std;
int main() {
int nums[2][4] = {{17, 24, 86, 35},
{23, 36, 10, 12}};

[Code] .....

I need modifying a program that should display the contents of the two- dimensional array, column by column and also row by row. I need to complete the program using a WHILE statement in the outer loops and a FOR statement in the nested loops.

View 1 Replies View Related

C++ :: Zero In List Contents And Multiset Contents

Apr 24, 2015

HTML Code:

So I insert values from a vector into a list and into a multiset, and I noticed zero is added to their contents! I had to do a whole lot of debugging to find out where the error was, how can i stop this thing? Code which generates such error...

infact i checked the content of vector ups to be sure that there was no zero in it, but after loading into list combi_t * head, it seems like there was a zero added and this is giving me errors when i call function master_roller...

Code:
void ins(combi_t * &testa, int &numero, int &num, int &no)
{ // if (ricerca(testa, numero) == 0)
//{
combi_t *temp = new combi_t;

temp->val0 = numero;
temp->val1 = num;
temp->val2 = no;
temp->nextPtr = 0;

[Code] ....

View 2 Replies View Related

C :: Transferring Contents Of One Character Array To Another Function

Feb 1, 2014

In the function *expandArrayList and *trimArrayList I need to allocate either more memory or less memory to the given array. Right now I am using malloc on a new array and then transferring the elements of the original array to the new array, freeing the original array, and then reassigning it to the new array so it's size and capacity are either bigger or smaller than the original. I feel like there has to be a simpler way to do this with either realloc or calloc, but I cant find sufficient information on either online. Would I be able to use either to make this easier and the code cleaner?

Also, these are character arrays that contain several character arrays inside them, so I was wondering what the best way to transfer the contents of one array to the other would be. I have gone between

Code:

//example
for (i=0; i<length; i++){
newarray[i] = oldarray[i];
}
and
Code: for (i=0; i<length; i++){
strcpy(newarray[i], oldarray[i]);
}

but I'm not sure which one (if either) should work.

The 'ArrayList.h' file that is included contains the structure ArrayList, as well as the function prototypes for the functions listed below.

Here is the structure in ArrayList.h:

Code:

#define DEFAULT_INIT_LEN 10
typedef struct ArrayList {
// We will store an array of strings (i.e., an array of char arrays)
char **array;

[Code] ....

Here is my code:

Code:

//included libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ArrayList.h"
//Create arraylist

[Code]....

View 3 Replies View Related

C++ :: How To Load Contents Of Text File Into Array

May 12, 2013

So I am working on a FUSE filesystem and I currently have the need to load the contents of a text file into an array.

My array initialization looks like: char array[max_entries][PATH_MAX] = {NULL}

The reason I want to pass it by reference is that I want my function to return two values essentially. One a specific char* and the other an array like I initialized. My function proto type looks like:

char* load_meta(char* list[max_entries][PATH_MAX], char* path, int mode);

How I'm trying to call the function:

someChar = load_meta(&array, path_name, 1);

Within the function I try to edit the array by deferenceing it once, like this:

strcpy(*list[i], file_entry); // This seg faults

View 8 Replies View Related

Visual C++ :: Resetting Contents Of Existing Array To 0?

Apr 13, 2015

Is there some quick way of resetting the contents of an existing array to 0? Just to be clear, I'm not initializing the array, it already exists, has content and needs to be reset at 0. Is there a faster way than the code below?

Code:
for(i=0;i<100;i++)myarray[i]=0;

View 6 Replies View Related

C++ :: Read A Text File And Store Contents Into 2D Array?

Aug 2, 2014

read a text file and store the file contents into a 2D array?

100 101 102 103 104 105
106 107 108 109 110 111
112 113 114 115 116 117
118 119 120 121 122 123
124 125 126 127 128 131

Here's my code:

const int ROWS = 5;
const int COLS = 6;
int array[ROWS][COLS];
ifstream inputFile;
inputFile.open("table.txt");

[code]....

When i run the program and try and display the array, it doesn't work.

View 1 Replies View Related

C++ :: Store User Input Then Display Array Contents?

Jan 10, 2015

I'm coding a hangman game. I'm trying to store user entries so i can output them to show the user what they have already entered. Problem is that it's not display anything at all.

I'm trying to store multiple characters of course, and then display all characters stored.

char guess[27]={0};
cin >> guess[26];
int hit=0;
for(int i=0; i<len; i++) {
if( guess[26] == hidden_word[i] ) {
hit++;
select_word[i] = guess[26];
if(strcmp(hidden_word,select_word) == 0) {

[code]....

EDIT: I also get the error - Stack around the variable 'guess' was corrupted. At the end of the game.

View 6 Replies View Related

C++ :: Store Contents Of Text File Into Char Array?

Apr 22, 2014

I am trying to store the contents of a text file into a char array. However the function i am using ifstream member function get(); seems to stop working when fed with certain characters. Is there another solution besides the get() function that will accept all types of characters from files?

char text[1000];
for (int i = 0; i <= textlen; ++i)
{
text[i] = text_in.get();
}

View 3 Replies View Related

C/C++ :: Creating Overloaded Cout That Will Print Contents Of Array

Sep 22, 2014

I need to create an overloaded cout that will print the contents of an array. So I can say output << a << endl;

And it will print the contents of the object a... which happens to be an array.

class info:
 
class List {
    public:  
        List();  
        bool empty(); //returns true of false if empty  
        void front(); //makes current position at beginning of list  
        void end(); //makes current position at the end of list  

[Code] ....

I understand this code, I am simply calling the size method from the program, but i don't know how to pass in the array so that i can print it line by line... simple syntax i am sure... but the whole thing is baffling me... I need to be able to call this on any variation of the class, so it cannot be specific to any one array.

View 4 Replies View Related

C/C++ :: How To Open A File And Store Contents Into Character Array

Jan 2, 2014

I am trying to store a large text file into an array so I can count the number of characters.

View 2 Replies View Related

C++ :: Modify Contents Of 1D Character Array - Converting To Upper Case

Feb 10, 2015

I am unsure how to write a function which modifies the content of the 1D character array and puts all of the letter it contains into uppercase. the following are the letters which i am trying to convert.

char text[MAX+1] = {'T', 'e', 's', 't', 'e', 'r', EOT};

The output to this should look like T E S T E R EOT

View 2 Replies View Related

C/C++ :: Read Contents From A File And Use As Size Of 2D Array - Error C2087 And C2133

Oct 20, 2014

Goal:
Read contents from a file and use as the size of a 2d array.

Problem:
C2087: Line 27 : "a1" : missing subscript
C2133: Line 27 : "a1" : unknown size

Code :
int rows;
int columns;
ifstream fObject("inputdata.dat");
fObject>>rows;
fObject>>columns;
char a1[rows][columns];

I am not sure why this is occurring, or if there is a better way to do it.

View 3 Replies View Related

C++ :: Reversing A String

Feb 26, 2015

I have this bit of code that reverses a string but there's a bit of it that I just don't understand:

int main()
{
char sentence[20]; //Create char array
void reverse( const char * const sPtr );
printf( "Enter your text please:
" );

[code]....

I just don't get how this bit results in the entered string being reverse.how to make code look like code.

View 6 Replies View Related

C :: Reversing A String Using Pointers

Nov 13, 2013

everything seems to be right in this program except that the puts() function does not print anything.

Code:

/*c program to reverse a string using pointer*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
}

[code]....

View 4 Replies View Related

C :: Reversing String Not Words

Oct 2, 2014

I am trying to write a program in C to reverse a string .

Example - This is America
output - America is This

I have thought of two ways of doing this question

1) using 2D arrays ( no pointer)
2) Using Strings and pointer

Code:
#include<stdio.h>
#include<string.h>
main()
}

[code]....

View 4 Replies View Related

C++ :: Reversing Linked List?

Sep 12, 2013

Code is:

#include<stdio.h>
struct node {
int info;
struct node *next,*prev;

[Code] ....

How to reverse a linked list .....

View 1 Replies View Related

C++ :: Reversing N Characters In A String?

Dec 26, 2013

I have a string like str="ABCDEFGHIJK";

need o/p like this str="CBAFEDIHGJK"

am getting "CBA" correctly after that its not printing anything.

int main()
{
string str="ABCDEFGHIJK";
char str1[10],rev[10];
int n=str.length(),count=0,c=3,k=0,j=0;

[Code].....

View 10 Replies View Related

C++ :: Reversing Five Digits Number

Mar 9, 2013

//Program to reverse 5 digit number

#include <stdio.h>
void main() {
int num, i = 0;
char rev[5];
printf("Enter any 5 digit number

[Code] ....

WHILE DISPLAYING i am not getting correct output. whats wrong in rev[] array.

View 7 Replies View Related

C++ :: Reversing String Using Seek / Tell?

Aug 8, 2012

I have an assignment that asks me to write a program which reads all lines from a text file (one by another), reverses them and write to the same file. I also have a pseudocode with which I have to work and it is as follows:

While the end of the file has not been reached

pos1 = current get position
Read a line.
If the line was successfully read
pos2 = current get position
Set put position to pos1.
Write the reversed line.
Set get position to pos2.

I have tried many things and the code misses letters. Below you can see my code:

Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
fstream file;
file.open("output.txt");

[code]....

This is the text with which I have to work:

Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.

And this is what I get:

bmal elttil a dah yraM
Itswons sa etihw saw eceelf
Antnew yraM taht erehwyreve d
T.og ot erus saw bmal eh

View 7 Replies View Related

C/C++ :: Reversing A Circular Doubly Linked List

Apr 25, 2015

I print the original list, then reverse it, then try to print it again. Here is what I get when I do that:

So that second line should print 2 1 4 5

Here is the reverse function:

void reverseCirListDeque(struct cirListDeque *q)
{
struct DLink *curr = q->Sentinel;
do{

[Code].....

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved