Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

algorythm problems..

Posted on 1997-04-29
3
273 Views
Last Modified: 2010-05-18
I have had problems with my algorythm, and now I can't get the debugger to work on this on. can you tell me what my problem is with the code, and how to go about solving it?
here is the code...
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>



main()
{
      static char ar[30];
      static char pr[30];
      static char br[30];
      static int vote;
      int x, y, z;
      int sw, b, a, c;
      strcpy(br,"elow!13%*$#!1Hllwo!");
      c = strlen(br);

      printf("Enter choice 1 or 2:\n");            
    printf("(1) To encrypt a phrase\n");
    printf("(2) To decrypt a phrase\n");
    scanf("%d", &vote);
      {
      
    if(vote==1)
            sw=0;
      fflush (stdin);

      if(sw==0)
            {
            printf("\nEnter the phrase you wish to have encoded...\n");
            gets(ar);
            a = strlen(ar);
            sw=1;

            printf("\npassword?\n");
            gets(pr);
            b = strlen(pr);
            y=0;
            }
      {

            for (x=1; x < a; x++)            /* increments x, while less than a*/
                  printf("%s", ar[x + 24]);            /* print the value of (x+24)*/
            for (y=1; y < b; y++)            /* same y increment*/
            {
                  if ( y > a)            /* if y is larger than the value of the password, then start over*/
                        y=0;}
                  printf("%s", pr[y + 24]);      /* print y+24*/
            for (z=1; z < c; z++)      /* same increment for z*/
                  printf("%s", br);      /* print the whole br array for values of z*/
      }

                  
            }
      

}
0
Comment
Question by:iamdwe
  • 2
3 Comments
 

Author Comment

by:iamdwe
ID: 1250171
Edited text of question
0
 
LVL 1

Accepted Solution

by:
prc earned 100 total points
ID: 1250172
I think you're having severe problems with C syntax.  It looks to me that you're not quite sure where curly brackets are supposed to go, so let's fix that first:

An if statement should look like this

  if (..some condition..)
  {
    .. a statement; ...
    .. another statement, maybe; ...
  }

A 'for' looks similar.  It's best to put in the curly brackets even if there is only one statement to remind you to use them when you add another one - there are several places in your code where it looks like you've forgotten them.

To be honest I'm not quite sure what this code is supposed to do (you didn't specify), but I'm pretty suspicious of the line

printf("%s", pr[y+24]);

This shouldn't even compile with a clever compiler, because what it means is "Take the character at (y+24) in the array 'pr' and print it as a string.".  It's meaningless, I'm afraid.

If you want to print characters, you should use '%c', not '%s'.  I'm guessing, but I suspect you want "pr[y]+24", not "pr[y+24]".  You want to get the character first, then add 24 to it.

Other things - the use of 'sw' looks dodgy.  You set it to zero if 'vote' is 1, but what is it if 'vote' isn't 1?  It must be initialised to something before use.  I can't follow the rest of that bit, I'm afraid.

Oh yes, think hard about how many times for(y=1; y<b; y++) loops round, and whether you shouldn't be starting with 0 as an array index.

I could write a clean version for you, but I'm not going to, because fixing these problems is the only way you can learn - I had to, years ago.  Cruel but kind, I'm afraid.

Hope this helps,

Paul
0
 

Author Comment

by:iamdwe
ID: 1250173
I didn't expect you to give me code, the answer was good enough!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
using pointers to pointers to write to a two dimensional array 16 94
How to jump to matching brace in eclipse editor ? 1 321
Problem to ASCII 1 197
Problem to show menu 5 93
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question